View Single Post
  #9   (View Single Post)  
Old 16th May 2008
yurtesen yurtesen is offline
Port Guard
 
Join Date: May 2008
Posts: 18
Default

Actually, that information is not correct unless I am mistaken. Int variables have a 32bit size and it doesnt matter if you compile your program on a 32bit or 64bit machine. One must use long int if 64bit int is wanted. 32bit registers do exist in 64bit machines too so there is no reason for the compiler to allocate twice the amount of space. As a repeatable example look at this tiny program compiled on 64bit OS.

Quote:
#include <stdio.h>

main() {
int a,c;
long int b,d;

a=2147483649;
c=32;
b=2147483649;
d=64;

printf("%d %ld %d %ld \n",a,b,c,d);

}
If you look at the object code (the source lines are visible if you use -pg -g switches while compiling and you will need profiled libraries in the system to be able to use -pg)

Quote:
main() {
400618: 55 push %rbp
400619: 48 89 e5 mov %rsp,%rbp
40061c: 48 83 ec 20 sub $0x20,%rsp
400620: e8 b3 fe ff ff callq 4004d8 <mcount@plt>
int a,c;
long int b,d;

a=2147483649;
400625: c7 45 e8 01 00 00 80 movl $0x80000001,0xffffffffffffffe8(%rbp)
c=32;
40062c: c7 45 ec 20 00 00 00 movl $0x20,0xffffffffffffffec(%rbp)
b=2147483649;
400633: c7 45 f0 01 00 00 80 movl $0x80000001,0xfffffffffffffff0(%rbp)
40063a: c7 45 f4 00 00 00 00 movl $0x0,0xfffffffffffffff4(%rbp)
d=64;
400641: 48 c7 45 f8 40 00 00 movq $0x40,0xfffffffffffffff8(%rbp)
400648: 00

printf("%d %ld %d %ld \n",a,b,c,d);
400649: 48 8b 45 f8 mov 0xfffffffffffffff8(%rbp),%rax
40064d: 8b 4d ec mov 0xffffffffffffffec(%rbp),%ecx
400650: 48 8b 55 f0 mov 0xfffffffffffffff0(%rbp),%rdx
400654: 8b 75 e8 mov 0xffffffffffffffe8(%rbp),%esi
400657: 49 89 c0 mov %rax,%r8
40065a: bf 88 07 40 00 mov $0x400788,%edi
40065f: b8 00 00 00 00 mov $0x0,%eax
400664: e8 2f fe ff ff callq 400498 <printf@plt>

}
I have made bold the first memory locations for int variables, e8 and ec, if you do a ec - e8 = 4 you can see that there is 4bytes difference when int variables are used meaning int variable occupies 4bytes in the memory. Whereas long int (64bit integer) variables f8 - f0 = 8 meaning they occupy 8bytes of memory space.

Also as you can see, the registers, rax,rdx (64bit registers) and ecx,esi (32bit registers) are separetely used depending on the variable size printed.

If you were right, and int variables occupied 64bits on 64bit architectures then the machine couldnt use 32bit registers to store them for printing, also the memory space used would be 8bytes instead of 4bytes.

Output:

Quote:
-2147483647 2147483649 32 64
As you can see the, 32bit int variable have overflowed and changed into a negative value. Because we have exceeded 32bit storage limit.

The only problem which I can see is that the memory alignment can force some small memory gaps however the compiler already does optimize for this this situation.

Honestly, I am quite tired of this discussion. If you think that using 32bit OS you are saving few bytes from 1gb-4gb memory then please go ahead. I just hope that you wouldnt suggest people who has <=4gb memory to use 32bit OS. Because that is a bad suggestion.

Thanks,
Evren
Reply With Quote