|
-
September 12th, 2006, 09:32 PM
#1
Little theoretical question
A little theorctical question about variable size in C++. I would like to know the difference between a int and a long. The range of int is -2147483647 and +2147483647 and a long int is the same thing. If the range is the same what is the difference?
-
September 12th, 2006, 09:52 PM
#2
Re: Little theoretical question
 Originally Posted by Geof
A little theorctical question about variable size in C++. I would like to know the difference between a int and a long. The range of int is -2147483647 and +2147483647 and a long int is the same thing. If the range is the same what is the difference?
The range of built-in integer types is implementation-defined. That is, it can vary between different architectures and operating systems. The only thing specified in the C++ Standard is that sizeof(int) <= sizeof(long).
For 32-bit windows OS, it is common for both int and long to be 4 bytes (thus giving the range you mentioned). However, if you want your programs to be portable to other platforms, you shouldn't rely on that.
Old Unix programmers never die, they just mv to /dev/null
-
September 13th, 2006, 02:08 AM
#3
Re: Little theoretical question
In addition to what HighCommander4 said, you can use numeric_limits<T> members (max?/min?) to find the limits on your specific platform.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
September 13th, 2006, 06:12 PM
#4
Re: Little theoretical question
Thank you for your awensers
-
September 14th, 2006, 03:06 AM
#5
Re: Little theoretical question
 Originally Posted by Geof
The range of int is -2147483647 and +2147483647
Which platform do you use?
There are probably platforms having such numeric limits, but if it was one of these platforms I would expect you to know the fact that numeric limits are compiler (or at least platform) dependent.
If your platform is x86-32 (two's complement representation without any trap representation and without any padding bit) and you don't use an obfuscated compiler, MIN_INT is probably (-2147483647-1).
That's a Good Reason to follow exterminator's advice.
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
-
September 14th, 2006, 05:30 AM
#6
Re: Little theoretical question
You can't rely on anything except that long is at least as long as short, and that int must be somewhere between short and long inclusive.
I think it may be required as a minimum that short is 16 bits and long 32 but even that I'm not sure.
In C99 there are types that indicate the minimum number of bits with a u prefix for unsigned. They may bring these into the new C++ standard (C++0x). Thus uint32_t is an unsigned int of at least 32 bits.
C++ differs from C with regard to overloading based on type. (C doesn't permit it). So if uint32_t is only a typedef, then you cannot be certain what would happen if you overloaded it. Note that if you are going to do lots of overloads based on numeric types, you can use one "traits-style" template to lead the way.
-
September 14th, 2006, 06:26 AM
#7
Re: Little theoretical question
A more practical answer:
You are now using a 32bit platform where int and long are the same size. However, in 5 years or so, 64bit platforms will be the mainstream, as the processors are already available and also the operating systems.
So, use int if you want that variable to be still 32 bit in the future.
Use long, if you want that variable to be 64 bit in the future.
Not what the others posted: You cannot rely on that your compiler will compile long as 64 bit and not event that it will compile int as 32 bit. It's just my experience that this is the case on most platforms.
-
September 14th, 2006, 06:49 AM
#8
Re: Little theoretical question
I'm not sure that the number of bits relates so much to the size of integers as the size of pointers, i.e. the address space.
The integral types that are likely to be affected:
size_t
off_type / off_t
ptrdiff_t
I'd be interested to know what will be used for 16-bit, 32-bit and 64-bit integers in 64-bit systems. They may keep it as short=16 bit, int=32 bit and long=64 bit. If not, what exactly will represent a 32-bit integer? And if that's short, what will be 16-bits?
Personally, I prefer the C99 notation.
-
September 14th, 2006, 07:21 AM
#9
Re: Little theoretical question
 Originally Posted by NMTop40
I'd be interested to know what will be used for 16-bit, 32-bit and 64-bit integers in 64-bit systems. They may keep it as short=16 bit, int=32 bit and long=64 bit.
All 64-bit platforms, I am working with (CC on Solaris/Sparc, aCC on HP-UX/PA-Risc/Itanium, xlC on AIX/Power) use that scheme as default. I think though that at least some of the compilers have options for overriding those sizes.
-
September 14th, 2006, 08:47 AM
#10
Re: Little theoretical question
Btw, -MIN_INT is equal to MIN_INT . Anyway, it's not safe to calculate with values around limits, actual limits should be stricter.
"Programs must be written for people to read, and only incidentally for machines to execute."
-
September 14th, 2006, 01:01 PM
#11
Re: Little theoretical question
 Originally Posted by NMTop40
I'd be interested to know what will be used for 16-bit, 32-bit and 64-bit integers in 64-bit systems. They may keep it as short=16 bit, int=32 bit and long=64 bit. If not, what exactly will represent a 32-bit integer? And if that's short, what will be 16-bits?
AFAIK, this LP64 model will be much used.
However, under Win64, compilers (at least Visual C++) will use LLP64 (32 bits int and long and 64 bits long long and pointers).
An interesting article:
http://www.usenix.org/publications/l...s/10.data.html
And google:
http://www.google.com/search?q=LLP64%20LP64%20ILP64
 Originally Posted by RoboTact
Btw, -MIN_INT is equal to MIN_INT
Not always... Though that's true with two's complement non-overflowing representations having no trap representations.
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
-
September 14th, 2006, 08:09 PM
#12
Re: Little theoretical question
 Originally Posted by NMTop40
I think it may be required as a minimum that short is 16 bits and long 32 but even that I'm not sure.
This is an interesting point. I always though that the standard imposes no such restrictions as minimum sizes but it appears otherwise:
 Originally Posted by The C++ Programming Language by Bjarne Stroustrup, Third Edition
... it is guaranteed that a char has at least 8 bits, a short and an int at least 16 bits, and a long at least 32 bits
Old Unix programmers never die, they just mv to /dev/null
-
September 15th, 2006, 02:26 AM
#13
Re: Little theoretical question
 Originally Posted by Geof
A little theorctical question about variable size in C++. I would like to know the difference between a int and a long. The range of int is -2147483647 and +2147483647 and a long int is the same thing. If the range is the same what is the difference?
The difference is between 16-bit real-mode compilers and 32-bit protected-mode compilers. Back before we had the flat memory model, and all memory addressing was 16-bit, an int was defined as two bytes where as on a 32-bit protected mode compiler and int is defined as four bytes. That's partly the reason we have longs and shorts. A long is always defined as 4 bytes no matter what the memory model, and a short is defined as two byes always. In C++, shorts and longs are portable across compilers while a int is not.
-Greg Dolley
-
September 15th, 2006, 02:35 AM
#14
Re: Little theoretical question
 Originally Posted by HighCommander4
This is an interesting point. I always though that the standard imposes no such restrictions as minimum sizes but it appears otherwise:
Yes.
To be more exact, the standard imposes conditions on numeric limits, such as:
MAX_INT must be at least 32767
MIN_INT must be at most -32767
etc.
It also requires that CHAR_BIT be at least 8
Knowing that POD type values only depend on their object representation, it's not hard to deduce that, even without any padding bit, an int must be at least 16 bits (i.e. sizeof(int)*CHAR_BIT >= 16), a long must be at least 32 bits, etc.
Of course, the useful guarantee is not the number of bits of integer types, but the guarantees imposed on their limits!
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
-
September 15th, 2006, 02:42 AM
#15
Re: Little theoretical question
 Originally Posted by greg_dolley
That's partly the reason we have longs and shorts.
long and short are far older than x86-16 and x86-32!
And, I don't think that an effect can precede a cause (except if you introduce machine times).
 Originally Posted by greg_dolley
A long is always defined as 4 bytes no matter what the memory model, and a short is defined as two byes always.
I hope you'll change your mind when you have a 64 bits machine in hands.
Fortunately, if you only use Windows, Microsoft choosed to respect the compatibility with your mind, using the LLP64 model for Win64.
But, AFAIK, the LP64 model (with 64 bits long) is far more popular on other platforms.
 Originally Posted by greg_dolley
In C++, shorts and longs are portable across compilers while a int is not.
-Greg Dolley
No.
Even with 64 bits machines there are several models ILP64, LP64 and LLP64.
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|