Article View: borland.public.cppbuilder.nativeapi
Article #14361Re: strtol question.
From: Alan Bellingham
Date: Mon, 18 Aug 2003 13:10
Date: Mon, 18 Aug 2003 13:10
53 lines
2524 bytes
2524 bytes
"Ed Mulroy [TeamB]" <ed@mulroyREMOVETHESECHARS.org> wrote:
>The documentation states that 0 is the return value from strtol when there
>is an error.
I think the OP realised that: "if the value is actually "0" or if its an
invalid number?"
>If you look in the source code for strtol you will be able to verify that it
>also sets the errno variable.
Sadly, although the source code indicates that it does, it doesn't work
for me either (BCB5 + SP):
#include <cerrno>
#include <iostream>
int main()
{
char* endptr = 0;
char const* data = "***";
long res = strtol(data, &endptr, 10);
int i = std::errno;
std::cout << res << " " << i << " " << ERANGE;
}
C:\>test
0 0 34
C:\>
(Temporary used just in case std::cout::operator<<() decided to muck
around with the value of errno.)
As far as my quick scan indicates, the only circumstance that causes
errno to be set is an actual overflow, and the return value is unlikely
to be zero in that case. This is as per the C Standard, and the client
does need to sanity check the input string, probably scanning from the
input value to endptr to see if there were any digits in the range,
something like this:
if (res == 0)
{
bool is_valid_number = false;
for (char const* p = data; p != endptr && !is_valid_number; ++p)
if (std::isdigit(*ptr))
is_valid_number = true;
}
(Not tested)
Alan Bellingham
--
alanb@episys.com
Technical Details
Message-ID:
<eqi1kvk5265bh6ocgr2p8i934dt2einqvd@4ax.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-01!sn-xit-09!supernews.com!news.maxwell.syr.edu!small1.nntp.aus1.giganews.com!border1.nntp.aus1.giganews.com!nntp.giganews.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<3f40b30b$1@newsgroups.borland.com> <3f40c083$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10cc67,ecc2b4647792de18 X-Google-Attributes: gid10cc67,public X-Google-ArrivalTime: 2003-08-18 06:38:49 PST From: Alan Bellingham <alanb@episys.com> Newsgroups: borland.public.cppbuilder.nativeapi Subject: Re: strtol question. Date: Mon, 18 Aug 2003 14:10:52 +0100 Reply-To: alanb@episys.com Message-ID: <eqi1kvk5265bh6ocgr2p8i934dt2einqvd@4ax.com> References: <3f40b30b$1@newsgroups.borland.com> <3f40c083$1@newsgroups.borland.com> X-Newsreader: Forte Agent 1.92/32.572 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 193.129.251.130 X-Trace: newsgroups.borland.com 1061212274 193.129.251.130 (18 Aug 2003 06:11:14 -0700) Lines: 53 Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-01!sn-xit-09!supernews.com!news.maxwell.syr.edu!small1.nntp.aus1.giganews.com!border1.nntp.aus1.giganews.com!nntp.giganews.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail