Memory Alignment
My compiler has four settings for memory alignment (8, 16, 32, 64 bits). Which one is preferred, 32 or 64?
Thanks,
Micah
November 20, 2000 05:48 PM
Do you know what memory alignment is? What compiler are you using?
Unless you have some specific reason to change it it''s usually not something you want to mess with. Most compilers default to "natural" alignment which is generally the best way to go unless you need a different alignment for compatibity reasons or for specialized multiprocessing reasons.
For most people a bigger alignment just results in wasted space. Smaller is better as long as you don''t go below the natural alignment of a type.
-Mike
Unless you have some specific reason to change it it''s usually not something you want to mess with. Most compilers default to "natural" alignment which is generally the best way to go unless you need a different alignment for compatibity reasons or for specialized multiprocessing reasons.
For most people a bigger alignment just results in wasted space. Smaller is better as long as you don''t go below the natural alignment of a type.
-Mike
Sorry Mike,
I should have mentioned this is for a 32bit Windows compiled for Pentium class machines. The default config for memory alignment for my compiler (Borland C++ 5.02) is byte. I would hope that you would agree that this is not a good choice.
So again I ask, (more clearly this time) what is the preferred memory alignment for Win32 apps on Pentium class machines, 32 or 64?
Micah
I should have mentioned this is for a 32bit Windows compiled for Pentium class machines. The default config for memory alignment for my compiler (Borland C++ 5.02) is byte. I would hope that you would agree that this is not a good choice.
So again I ask, (more clearly this time) what is the preferred memory alignment for Win32 apps on Pentium class machines, 32 or 64?
Micah
You will have to test your program to see what is best. Sometime 32 bit alignment will result in faster reads and writes from memory, but it also increases your binary size because you are padding some vairables. This additional size might be enough to push your core program out of CPU cache therefore making it slower that the byte aligned variables.
So as you can see it depends on the program. If it makes no diffrence leave it alone.
So as you can see it depends on the program. If it makes no diffrence leave it alone.
November 21, 2000 12:28 PM
Actually I would not agree that byte alignment is necessarily a bad choice.
I would try running the following program without specifying any switches that affect alignment:
Assuming 32-bit Borland C++ uses 4-byte longs you will probably get either 5 or 8 as output.
If you get 8 then your compiler is aligning the long at it''s natural alignment - this is usually a good thing. If you get 5 then you *may* get a small perf increase at the cost of using more memory by explicitely setting the alignment to 32-bit. As I said before unless you have some reason to change the alignment its usually best not to muck with it.
VC on NT and gcc on SunOS both give 8 btw.
-Mike
I would try running the following program without specifying any switches that affect alignment:
struct test{ char a; long b;};void main(){ printf("%d\n", sizeof(test));}
Assuming 32-bit Borland C++ uses 4-byte longs you will probably get either 5 or 8 as output.
If you get 8 then your compiler is aligning the long at it''s natural alignment - this is usually a good thing. If you get 5 then you *may* get a small perf increase at the cost of using more memory by explicitely setting the alignment to 32-bit. As I said before unless you have some reason to change the alignment its usually best not to muck with it.
VC on NT and gcc on SunOS both give 8 btw.
-Mike
You should always use largest size for memory alignment.
In case of VC6 - 8 byte is enough (it has no intrinsic datatypes large then 64bit).
For Intel C++ compiler - 16 bytes (if you want to use SSE).
If you think, that because of the alignment you wasting too much memory - just rearrange members in the critical structures & classes.
In case of VC6 - 8 byte is enough (it has no intrinsic datatypes large then 64bit).
For Intel C++ compiler - 16 bytes (if you want to use SSE).
If you think, that because of the alignment you wasting too much memory - just rearrange members in the critical structures & classes.
quote: Original post by Serge K
You should always use largest size for memory alignment.
In case of VC6 - 8 byte is enough (it has no intrinsic datatypes large then 64bit).
For Intel C++ compiler - 16 bytes (if you want to use SSE).
If you think, that because of the alignment you wasting too much memory - just rearrange members in the critical structures & classes.
I don't understand that. Why must you always use the largest size for memory alignment? I thought that the current crop of processors were 32 bit, therefore the optimum alignment would be 4 bytes. 8 bytes would only be necessary if you were intending to have your programs ported to a 64 bit OS (sometime in the future).
Or am I mistaken? Any clarifications will be appreciated.
Edited by - NuffSaid on December 2, 2000 5:24:53 AM
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
quote: Original post by NuffSaid
I thought that the current crop of processors were 32 bit, therefore the optimum alignment would be 4 bytes. 8 bytes would only be necessary if you were intending to have your programs ported to a 64 bit OS (sometime in the future).
You forgot about double.
Also you need 64bit alignment for MMX.
And you must use 128bit alignment with SSE.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement