atol of a value > INT_MAX

A

A. Farber

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
 
S

santosh

A. Farber said:
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).
 
S

santosh

santosh said:
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.
 
B

Bill Medland

A. Farber said:
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).
 
C

CBFalconer

A. Farber said:
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?
 
B

Barry Schwarz

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
 
A

A. Farber

Hi,

Barry said:
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/cvsweb/src/sys/sys/limits.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
 
K

Keith Thompson

A. Farber said:
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.
 
B

Barry Schwarz

$ 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top