LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Gentoo (http://www.linuxquestions.org/questions/gentoo-87/)
-   -   gcc 64-bit 4 byte int (http://www.linuxquestions.org/questions/gentoo-87/gcc-64-bit-4-byte-int-4175438078/)

fakie_flip 11-21-2012 02:35 AM

gcc 64-bit 4 byte int
 
Is there a gcc flag I can use to tell it to use only 4 bytes for int on a 64-bit machine instead of 8 bytes?

pan64 11-21-2012 04:15 AM

why do you need that? see here: http://crasseux.com/books/ctutorial/...variables.html and here: http://www.gnu.org/software/libc/man.../Integers.html

fakie_flip 11-21-2012 06:29 AM

Because using 8 bytes for an int is not needed if the same program will run fine with 4 bytes in a 32 bit distro. More of my ram is used than needed.

millgates 11-21-2012 06:55 AM

What makes you think your int is 64bit?
If you need specific width, just use types from stdint.h, as suggested by pan64

fakie_flip 11-21-2012 03:20 PM

1) Is it only the long datatype that differs in size between 32 bit and 64 bit?

2) Then why does a 64 bit OS have higher memory requirements than the same OS in 32 bit?

johnsfine 11-21-2012 03:30 PM

Quote:

Originally Posted by fakie_flip (Post 4834270)
2) Then why does a 64 bit OS have higher memory requirements than the same OS in 32 bit?

1) X86 instructions in machine language vary in length from single byte instructions up to quite long (I'm not sure of the max). Average instruction length is larger in x86-64 than in 32-bit x86. That is partially balanced by some situations in which the compiler emits fewer instructions in x86-64 than in 32-bit for the same C code. But only partially balanced. On average, the same code compiles larger on x86-64 than 32-bit.

2) Pointers are twice as big in x86-64 as in x86. In many programs, a large fraction of the data is pointers (rather than basic types like int, char, double, etc.).

3) Considering a whole OS, rather than just a program, you should also consider that it is common to run a few 32-bit programs on a 64-bit OS, but you can't run any 64-bit programs on a 32-bit OS. A 32-bit program and 64-bit program running at the same time that use shared libraries compiled from the same source code, can't use the same binary shared libraries. So running more than one bit size of application means more memory is used by the whole system for shared libraries.

4) If I understand correctly (I'm not sure of this one) the exception handling for C++ for x86-64 is a totally redesigned system meant to minimize the run time overhead of having exception handling (the time it take to not have an exception) at the expense of both memory size and the execution time when you actually do have an exception.

Lots of minor reasons as well, such as a more layered page table structure in the kernel for mapping virtual addresses to physical addresses. (Compared to 32-bit non PAE, the page tables also have half capacity, so you need twice as many. 32-bit PAE has the same capacity per page table as x86-64).

Quote:

Originally Posted by fakie_flip (Post 4833812)
Is there a gcc flag I can use to tell it to use only 4 bytes for int on a 64-bit machine instead of 8 bytes?

int is 4-bytes by default in gcc for x86-64.

There is a gcc switch -m32 that you can use on a 64-bit Linux (assuming 32-bit support is installed) to build your entire application as 32-bit, so pointers and longs are 32-bit just as on a 32-bit Linux.

If your application manipulates mainly pointers (rather than ints or doubles, etc.) and doesn't need more than 4GB per process. It may take significantly less memory and less CPU time run as a 32-bit application under a 64-bit OS than run as a 64-bit application.

It may seem bizarre to non programmers, but a lot of programs do manipulate pointers far more than actual data.

Most programs have no need to exceed the 4GB limit on a 32-bit program in a 64-bit OS. Even if the whole system has and needs far more than 4GB of physical ram, individual processes usually need less than 4GB of virtual ram each.

fakie_flip 11-24-2012 12:32 AM

Ok, I knew a lot of that except the instruction code loaded into memory is larger. I'm asking if long is the only datatype that differs in default size (except pointers, you mentioned them already)?

millgates 11-24-2012 04:03 AM

The only thing the standard says about this is that char <= short <= int <= long <= long long. Other than that, it's platform (CPU, OS, compiler) specific. If you want to write portable code, you should never assume anything about the size of the basic int types.
You may also find this interesting.

johnsfine 11-24-2012 08:56 AM

Quote:

Originally Posted by fakie_flip (Post 4835889)
I'm asking if long is the only datatype that differs in default size (except pointers, you mentioned them already)?

There are types such as size_t and ptr_diff_t that (in ordinary architectures) need to be the same size as pointers even though they are not pointers. So in x86-64 those are also 8 bytes long.


All times are GMT -5. The time now is 12:24 PM.