473,750 Members | 2,190 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4724
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.comwro te:
>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=2147483 647,
LONG_MAX=214748 3647

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(tabl e *id)
{
table *ptable;

/* find a table at the address id, but DO NOT dereference id!
*/
TAILQ_FOREACH(p table, &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(&ptabl e->info, "&table%u=" , ptable);

TAILQ_INIT(&pta ble->kibitzers);

TAILQ_INSERT_TA IL(&tables, ptable, entry);
ntables++;

info("create_ta ble->%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.comwri tes:
[...]
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_Keit h) 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=216250822 4, atoi=2147483647 , atol=2147483647 , INT_MAX=2147483 647,
LONG_MAX=21474 83647

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(tab le *id)
{
table *ptable;

/* find a table at the address id, but DO NOT dereference id!
*/
TAILQ_FOREACH(p table, &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(&ptabl e->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(&pta ble->kibitzers);

TAILQ_INSERT_TA IL(&tables, ptable, entry);
ntables++;

info("create_ta ble->%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
10056
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 digit to long to
16
5137
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. And K&R2 mentions "signed extension" everywhere. Reading some old clc posts, I've beginning to realize that these books are over-generalizing the topic. I am just wondering what the difference between the following pairs of terms are: 1)...
40
18477
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 that program, I honestly don't understand what it means, please help. Thanks.
2
3712
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 */ } else { /* exit with error message */
11
5676
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 a single interface: void setInfo(double); // store info double info(); // retrieve info and use
5
2241
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++ compiler. Using vc++, I don't get this behavior. Anyone have an explanation for this? //////////////////////////////////// void test(string sInput) { int iSum; char cCurr;
7
3463
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
6785
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 bytes. The remainder of the standard integer types are general purpose. Their only requirement is to satisfy a minimum range of values, and also int "has the natural size suggested by the architecture of the execution environment". What are the...
42
3547
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
9000
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8838
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9577
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9396
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9256
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6804
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6081
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4713
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4887
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.