Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > atol of a value > INT_MAX

Reply
Thread Tools

atol of a value > INT_MAX

 
 
A. Farber
Guest
Posts: n/a
 
      11-30-2006
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

 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      11-30-2006
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).

 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      11-30-2006
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.

 
Reply With Quote
 
Bill Medland
Guest
Posts: n/a
 
      11-30-2006
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
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      12-01-2006
"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>


 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      12-01-2006
On 30 Nov 2006 08:34:52 -0800, "A. Farber"
<(E-Mail Removed)> 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));


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
 
Reply With Quote
 
A. Farber
Guest
Posts: n/a
 
      12-01-2006
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

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-01-2006
"A. Farber" <(E-Mail Removed)> writes:
[...]
> 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) http://www.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      12-02-2006
On 1 Dec 2006 00:46:58 -0800, "A. Farber" <(E-Mail Removed)>
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
__getslice__ passed INT_MAX rather than sys.maxint for missing endpoint? Dave Huang Python 1 03-27-2005 11:10 AM
string.atoi and string.atol broken? Mike Moum Python 19 01-27-2005 08:35 AM
INT_MAX/MIN Roopa C Programming 4 08-16-2004 03:36 PM
what are the reverse functions of atof( ), atoi( ) and atol( ) ? Sharon C Programming 4 11-17-2003 10:13 AM
converting a char* to long using atol does not work :( Gizmo C++ 5 09-08-2003 07:25 PM



Advertisments