atol of a value > INT_MAX

Discussion in 'C Programming' started by A. Farber, Nov 30, 2006.

  1. A. Farber

    A. Farber Guest

    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
     
    A. Farber, Nov 30, 2006
    #1
    1. Advertising

  2. A. Farber

    santosh Guest

    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).
     
    santosh, Nov 30, 2006
    #2
    1. Advertising

  3. A. Farber

    santosh Guest

    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.
     
    santosh, Nov 30, 2006
    #3
  4. A. Farber

    Bill Medland Guest

    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
     
    Bill Medland, Nov 30, 2006
    #4
  5. A. Farber

    CBFalconer Guest

    "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>
     
    CBFalconer, Dec 1, 2006
    #5
  6. On 30 Nov 2006 08:34:52 -0800, "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));


    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
     
    Barry Schwarz, Dec 1, 2006
    #6
  7. A. Farber

    A. Farber Guest

    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/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

    --
    http://preferans.de
     
    A. Farber, Dec 1, 2006
    #7
  8. "A. Farber" <> 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.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Dec 1, 2006
    #8
  9. On 1 Dec 2006 00:46:58 -0800, "A. Farber" <>
    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
     
    Barry Schwarz, Dec 2, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gizmo
    Replies:
    5
    Views:
    434
    llewelly
    Sep 8, 2003
  2. Sharon
    Replies:
    4
    Views:
    1,023
    CBFalconer
    Nov 17, 2003
  3. Mike Moum

    string.atoi and string.atol broken?

    Mike Moum, Jan 25, 2005, in forum: Python
    Replies:
    19
    Views:
    683
    Peter Otten
    Jan 27, 2005
  4. Dave Huang
    Replies:
    1
    Views:
    386
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
    Mar 27, 2005
  5. Roopa

    INT_MAX/MIN

    Roopa, Aug 11, 2004, in forum: C Programming
    Replies:
    4
    Views:
    888
    Dan Pop
    Aug 16, 2004
Loading...

Share This Page