473,408 Members | 1,762 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

odd behavior from atol

I'm getting odd output from atol in a for loop. Entering a ten-digit
string I get different results for char and atol(char). If I drop iSum
the output seems to be okay. This happens using the g++ compiler. Using
vc++, I don't get this behavior. Anyone have an explanation for this?

////////////////////////////////////
void test(string sInput)
{
int iSum;
char cCurr;

sum = 0;

for (int i = 0; i < sInput.length(); i++)
{
cCurr = sInput.at(i);
cout << cCurr << " : " << atol(& cCurr) << endl;
iSum += (i + 1) * atol(& cCurr);
}
}

int main()
{
string sInput;

cout << "Enter test string: ";
cin >sInput;

test(sInput);

return 0;
}

////////////////////////////////////

jon@linux:~/projects/test./test
Enter test string: 0201530821
0 : 0
2 : 2
0 : 0
1 : 1
5 : 5
3 : 3
0 : 3 //!!
8 : 8
2 : 2
1 : 1
Jan 31 '07 #1
5 2223
Jon Cosby wrote:
I'm getting odd output from atol in a for loop. Entering a ten-digit
string I get different results for char and atol(char). If I drop iSum
the output seems to be okay. This happens using the g++ compiler. Using
vc++, I don't get this behavior. Anyone have an explanation for this?

////////////////////////////////////
void test(string sInput)
{
int iSum;
char cCurr;

sum = 0; // That's iSum!!
Jan 31 '07 #2
Jon Cosby wrote:
I'm getting odd output from atol in a for loop. Entering a ten-digit
string I get different results for char and atol(char). If I drop iSum
the output seems to be okay. This happens using the g++ compiler.
Using vc++, I don't get this behavior. Anyone have an explanation for
this?
Undefined behaviour. Look it up. You're lucky your program didn't
format your hard drive and didn't send nasty e-mails to your friends
on your behalf.
>
////////////////////////////////////
void test(string sInput)
{
int iSum;
char cCurr;

sum = 0;

for (int i = 0; i < sInput.length(); i++)
{
cCurr = sInput.at(i);
cout << cCurr << " : " << atol(& cCurr) << endl;
'atol' expects an {_array_ of char} that terminates with a null char,
'\0', and you're supplying it with the address of a single char. Your
code has undefined behaviour, so anything is possible.

You can correct it by declaring 'cCurr' an array, initialising it to
all zeros and the overriding only the first one with the return value
of 'at'.
iSum += (i + 1) * atol(& cCurr);
}
}

int main()
{
string sInput;

cout << "Enter test string: ";
cin >sInput;

test(sInput);

return 0;
}

////////////////////////////////////

jon@linux:~/projects/test./test
Enter test string: 0201530821
0 : 0
2 : 2
0 : 0
1 : 1
5 : 5
3 : 3
0 : 3 //!!
8 : 8
2 : 2
1 : 1
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 31 '07 #3
Jon Cosby wrote:
I'm getting odd output from atol in a for loop. Entering a ten-digit
string I get different results for char and atol(char). If I drop iSum
the output seems to be okay. This happens using the g++ compiler. Using
vc++, I don't get this behavior. Anyone have an explanation for this?

////////////////////////////////////
void test(string sInput)
{
int iSum;
char cCurr;

sum = 0;

for (int i = 0; i < sInput.length(); i++)
{
cCurr = sInput.at(i);
Belt and suspeners here, eh? Since the loop goes from 0 to
sInput.length(), you won't get an index out of bounds. But never mind...
cout << cCurr << " : " << atol(& cCurr) << endl;
atol expects a null-terminated character array. This code passes the
address of a char variable, so the function looks at that char and at
whatever comes after it in memory. Try this instead:

char cCurr[2] = {};

Now stuff your character into cCurr[0].

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Jan 31 '07 #4
Jon Cosby wrote:
I'm getting odd output from atol in a for loop. Entering a ten-digit
string I get different results for char and atol(char). If I drop iSum
the output seems to be okay. This happens using the g++ compiler. Using
vc++, I don't get this behavior. Anyone have an explanation for this?
Your code invokes undefined behavior.
////////////////////////////////////
void test(string sInput)
{
int iSum;
char cCurr;

sum = 0;

for (int i = 0; i < sInput.length(); i++)
{
cCurr = sInput.at(i);
cout << cCurr << " : " << atol(& cCurr) << endl;
atol expects a pointer to the first element of a zero terminated array of
char ("C style string"), and you only give it the address of a single char.
atol will try to read on in the memory beyond the variable until it finds
something it can interpret as a '\0' character.
iSum += (i + 1) * atol(& cCurr);
}
}

int main()
{
string sInput;

cout << "Enter test string: ";
cin >sInput;

test(sInput);

return 0;
}

////////////////////////////////////

jon@linux:~/projects/test./test
Enter test string: 0201530821
0 : 0
2 : 2
0 : 0
1 : 1
5 : 5
3 : 3
0 : 3 //!!
8 : 8
2 : 2
1 : 1
Jan 31 '07 #5
On Wed, 31 Jan 2007 13:21:30 -0500, "Victor Bazarov" <v.********@comAcast.net>
wrote:
>'atol' expects an {_array_ of char} that terminates with a null char,
'\0', and you're supplying it with the address of a single char. Your
code has undefined behaviour, so anything is possible.
Well, not exactly. atol expects an array of char that terminates with a
character that is for which isspace() returns nonzero, or that violates the
sequence ('+'|'-'|'')('0x'|'0X'|'')(['0'-'9']|['a'-'z']|['A'-'Z'])+

That's probably why the OP's program didn't immediately crash and burn. The
chances that a pattern-violating character is in the immediate vicinity of the
buffer is quite high, and reading from that memory doesn't corrupt any other
data structures.

-dr
Jan 31 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Mike Moum | last post by:
I think there may be a bug in string.atoi and string.atol. Here's some output from idle. > Python 2.3.4 (#2, Jan 5 2005, 08:24:51) > on linux2 > Type "copyright", "credits" or "license()"...
5
by: Gizmo | last post by:
Im not sure if this is a c or c++ question so I apologise if im in the wrong place. I want to convert a char* to a long. However the number that I want to convert appears to be one...
4
by: Sharon | last post by:
hi all what are the reverse functions of atof( ), atoi( ) and atol( ) ? I want to change sth to string thx!
19
by: E. Robert Tisdale | last post by:
In the context of the comp.lang.c newsgroup, the term "undefined behavior" actually refers to behavior not defined by the ANSI/ISO C 9 standard. Specifically, it is *not* true that "anything can...
8
by: sapnsapn | last post by:
There is a statement in c code I am reviewing filenum = atoll(item->d_name + strlen(msgid_with_append_str) + 1); In a certain snapshot, item->d_name = 050707143827.AAAA.11810.00000001...
8
by: A. Farber | last post by:
Hello, I have this simple program: #include <stdio.h> #include <stdlib.h> int main() { char *args = "2162508224"; printf("args=%s, atoi=%lu, atol=%lu\n",
31
by: Simply_Red | last post by:
i'm sorry i posted this in other groupes, and i didn't see it, and as this group is most actif, i repost it here, and sorry for mutliposting: Hi, i'm using VC6, i have this declaration: ...
12
by: Rajesh S R | last post by:
Can anyone tell me what is the difference between undefined behavior and unspecified behavior? Though I've read what is given about them, in ISO standards, I'm still not able to get the...
28
by: v4vijayakumar | last post by:
#include <string> #include <iostream> using namespace std; int main() { string str; str.resize(5); str = 't';
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.