473,395 Members | 1,726 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,395 software developers and data experts.

atol of a value > INT_MAX

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",
args, atoi(args), atol(args));
}

which prints (tested on OpenBSD and RHE Linux):

args=2162508224, atoi=2147483647, atol=2147483647

I understand that the "wrong" 2147483647 value is the

#define INT_MAX 0x7fffffff

from the /usr/include/sys/limits.h. But why is the last value wrong
too? I was expecting the atoi() to fail, but atol() to work fine...

Thank you
Alex
--
http://preferans.de

Nov 30 '06 #1
8 4691
A. Farber wrote:
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",
args, atoi(args), atol(args));
}

which prints (tested on OpenBSD and RHE Linux):

args=2162508224, atoi=2147483647, atol=2147483647

I understand that the "wrong" 2147483647 value is the

#define INT_MAX 0x7fffffff

from the /usr/include/sys/limits.h. But why is the last value wrong
too? I was expecting the atoi() to fail, but atol() to work fine...
On your platform it seems, sizeof(long) == sizeof(int).

Nov 30 '06 #2
santosh wrote:
A. Farber wrote:
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",
args, atoi(args), atol(args));
}

which prints (tested on OpenBSD and RHE Linux):

args=2162508224, atoi=2147483647, atol=2147483647

I understand that the "wrong" 2147483647 value is the

#define INT_MAX 0x7fffffff

from the /usr/include/sys/limits.h. But why is the last value wrong
too? I was expecting the atoi() to fail, but atol() to work fine...

On your platform it seems, sizeof(long) == sizeof(int).
Correction:
Apparently, LONG_MAX for your implementation is less than 2162508224.

You're also invoking undefined behaviour. Use the more robust strtol()
instead.

Nov 30 '06 #3
A. Farber wrote:
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",
args, atoi(args), atol(args));
}

which prints (tested on OpenBSD and RHE Linux):

args=2162508224, atoi=2147483647, atol=2147483647

I understand that the "wrong" 2147483647 value is the

#define INT_MAX 0x7fffffff

from the /usr/include/sys/limits.h. But why is the last value wrong
too? I was expecting the atoi() to fail, but atol() to work fine...
Why?

I would have expected atol to have failed in those environments (ill-defined
as they are).
>
Thank you
Alex
--
http://preferans.de
--
Bill Medland
Nov 30 '06 #4
"A. Farber" wrote:
>
I have this simple program:

#include <stdio.h>
#include <stdlib.h>

int main() {
char *args = "2162508224";
printf("args=%s, atoi=%lu, atol=%lu\n",
args, atoi(args), atol(args));
}

which prints (tested on OpenBSD and RHE Linux):

args=2162508224, atoi=2147483647, atol=2147483647

I understand that the "wrong" 2147483647 value is the

#define INT_MAX 0x7fffffff

from the /usr/include/sys/limits.h. But why is the last value wrong
too? I was expecting the atoi() to fail, but atol() to work fine...
Did you check the value of LONG_MAX?

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Dec 1 '06 #5
On 30 Nov 2006 08:34:52 -0800, "A. Farber"
<Al**************@gmail.comwrote:
>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",
args, atoi(args), atol(args));
Neither function returns an unsigned value in spite of the %lu.
}

which prints (tested on OpenBSD and RHE Linux):

args=2162508224, atoi=2147483647, atol=2147483647

I understand that the "wrong" 2147483647 value is the
Since the value cannot be represented, the behavior is undefined. This
is a very undesirable manifestation of undefined behavior unless is
documented as an extension on your system.
>
#define INT_MAX 0x7fffffff

from the /usr/include/sys/limits.h. But why is the last value wrong
too? I was expecting the atoi() to fail, but atol() to work fine...
What is LONG_MAX or sizeof(long) on your system.
>
Thank you
Alex

Remove del for email
Dec 1 '06 #6
Hi,

Barry Schwarz wrote:
What is LONG_MAX or sizeof(long) on your system.
thank you all. The LONG_MAX is in fact same as INT_MAX,
I didn't think of that:

http://www.openbsd.org/cgi-bin/cvswe...mits.h?rev=1.6

$ cat atoi.c
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int main() {
char *args = "2162508224";

printf("args=%s, atoi=%ld, atol=%ld, INT_MAX=%ld,
LONG_MAX=%ld\n",
args, atoi(args), atol(args), INT_MAX, LONG_MAX);
}

$ ./atoi
args=2162508224, atoi=2147483647, atol=2147483647, INT_MAX=2147483647,
LONG_MAX=2147483647

Actually my problem is totally different - a unique id
for the newly allocated members of my datastructure:

I have a doubly linked list of tables (at which the players
of my web game can sit down) and for each newly created
table I need a unique id.

I've decided to abuse the address of the newly malloced
table struct for that, i.e. when I create a new table, I return
smth. like printf("&table=%u", ptable); and when a player
wants to join that particular table, (s)he provides me that
address back as string. I go through my doubly linked list
of tables and look for that address:

/* find a table with this id (is a void* pointer) or create a new one
*/
table*
find_table(table *id)
{
table *ptable;

/* find a table at the address id, but DO NOT dereference id!
*/
TAILQ_FOREACH(ptable, &tables, entry)
if (ptable == id)
return ptable;

return create_table();
}

table*
create_table()
{
table *ptable;
unsigned i;

if ((ptable = calloc(1, sizeof(*ptable))) == NULL)
die("Can't allocate memory for a new table");

xsprintf(&ptable->info, "&table%u=", ptable);

TAILQ_INIT(&ptable->kibitzers);

TAILQ_INSERT_TAIL(&tables, ptable, entry);
ntables++;

info("create_table->%u", ptable);
return ptable;
}

Now I've got bitten by this too big values issue...
I wonder what to use instead of long type...

Regards
Alex

--
http://preferans.de

Dec 1 '06 #7
"A. Farber" <Al**************@gmail.comwrites:
[...]
I have a doubly linked list of tables (at which the players
of my web game can sit down) and for each newly created
table I need a unique id.

I've decided to abuse the address of the newly malloced
table struct for that, i.e. when I create a new table, I return
smth. like printf("&table=%u", ptable); and when a player
wants to join that particular table, (s)he provides me that
address back as string. I go through my doubly linked list
of tables and look for that address:
[...]

Probably you're using sprintf() rather than printf().

The format for printing a pointer is "%p". It's defined for void*, so
you need to convert the pointer value. For example:

char unique_id[BIG_ENOUGH];
sprintf(unique_id, "%p", (void*)&table);

The string printed by "%p" is implementation-defined; the only real
guarantee is that scanf with "%p" will give you back the same pointer.
Actually, it doesn't even guarantee that; it only guarantees that the
pointer will compare equal to the original one (if it was printed
during the same execution of the program).

This makes it impossible to compute BIG_ENOUGH portably, but it
shouldn't be difficult to figure it out for a given implementation.
CHAR_BIT * sizeof(void*) should be more than enough on any reasonable
implementation.

It's possible that the same pointer value will have multiple
representations, so you're not *really* guaranteed that your ids will
be unique, but in practice that shouldn't be an issue. And if you
have two different copies of the structure that refers to a given
table, they'll have different addresses.

But there's a much simpler way to generate unique ids. Just add a
member to your table structure, and keep a global counter of tables
allocated. Every time you allocate a new table structure, increment
the global count and assign it to the unique_id field. Use an integer
type big enough to hold the maximum count of tables you expect. This
also has the advantage that you can write the structure (and the
global counter) to a file and retrieve it next time you run the
program; pointer values (addresses) don't make sense across multiple
executions.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 1 '06 #8
On 1 Dec 2006 00:46:58 -0800, "A. Farber" <Al**************@gmail.com>
wrote:
>$ cat atoi.c
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int main() {
char *args = "2162508224";

printf("args=%s, atoi=%ld, atol=%ld, INT_MAX=%ld,
LONG_MAX=%ld\n",
args, atoi(args), atol(args), INT_MAX, LONG_MAX);
You didn't take the hint. atoi does not return a long; INT_MAX is not
a long. If you insist on lying to printf, don't complain about
unexpected results.

And the call to atoi still invokes undefined behavior.
>}

$ ./atoi
args=2162508224, atoi=2147483647, atol=2147483647, INT_MAX=2147483647,
LONG_MAX=2147483647

Actually my problem is totally different - a unique id
for the newly allocated members of my datastructure:

I have a doubly linked list of tables (at which the players
of my web game can sit down) and for each newly created
table I need a unique id.

I've decided to abuse the address of the newly malloced
table struct for that, i.e. when I create a new table, I return
smth. like printf("&table=%u", ptable); and when a player
wants to join that particular table, (s)he provides me that
address back as string. I go through my doubly linked list
of tables and look for that address:

/* find a table with this id (is a void* pointer) or create a new one
*/
table*
find_table(table *id)
{
table *ptable;

/* find a table at the address id, but DO NOT dereference id!
*/
TAILQ_FOREACH(ptable, &tables, entry)
if (ptable == id)
return ptable;

return create_table();
}

table*
create_table()
{
table *ptable;
unsigned i;

if ((ptable = calloc(1, sizeof(*ptable))) == NULL)
Since your struct contains a pointer, initializing it to all bits zero
is not necessarily a good idea.
die("Can't allocate memory for a new table");

xsprintf(&ptable->info, "&table%u=", ptable);
There is not a standard function. Is there some reason sprintf won't
work for you. For converting pointer values to strings, use %p and
cast the pointer to void*.
>
TAILQ_INIT(&ptable->kibitzers);

TAILQ_INSERT_TAIL(&tables, ptable, entry);
ntables++;

info("create_table->%u", ptable);
return ptable;
}

Now I've got bitten by this too big values issue...
I wonder what to use instead of long type...
Why are trying to convert the value to an integer type? Are you doing
arithmetic on it? Is there a problem with keeping it as a string? If
all you need to do is compare values, prepending '0'-s in front of a
short string will let you strcmp or memcmp.

Does you compiler support any integer type larger than long (or
unsigned long), even if it is not the new standard type long long?
Remove del for email
Dec 2 '06 #9

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

Similar topics

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...
16
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. ...
40
by: Matt | last post by:
Alright, this is puzzling me. Here's what it says: Exercise 1-7. Write a program to print the value of EOF. Which is from the book "The C Programming Language". How would I go about writing...
2
by: Marlene Stebbins | last post by:
I am entering numbers into my program from the command line. I want to check whether they are > INT_MAX. Sounds simple, but I've discovered that if(x <= INT_MAX) { /* use x in some calculation...
11
by: Bo Peng | last post by:
Dear C++ experts, I need to store and retrieve a meta information that can be int or double. The program would be significantly simpler if I can handle two types uniformly. Right now, I am using...
5
by: Jon Cosby | last post by:
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++...
7
by: dana_livni2000 | last post by:
how do i print the acuale byte sequence that represent a vairable (let say a char - i want to see the 8 bits). thanks dana
130
by: euler70 | last post by:
char and unsigned char have specific purposes: char is useful for representing characters of the basic execution character set and unsigned char is useful for representing the values of individual...
42
by: polas | last post by:
Afternoon all, I have some code (which I thought was OK, but now appreciate is probably not by the standard) which contains int a; ...... if (a==NULL) { ....
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.