Am I right to assume that compilers will pad out to a multiple of 4 on x86 platforms?
Is there a way, such a macro, to determine the size?
Am I right to assume that compilers will pad out to a multiple of 4 on x86 platforms?
Is there a way, such a macro, to determine the size?
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
This is compiler dependent, but usually on x86, a char will be 1-byte aligned, shorts 2-byte aligned, ints/floats 4-byte aligned.
bit∙hub [bit-huhb] n. A source and destination for information.
By default, I don't know.
I know you can specify an alignment with #pragma pack down to a 1-byte boundary up to a 16-byte boundary (powers of 2).
Data structure alignment - Wikipedia, the free encyclopedia <-- looks like it differs by the type within the struct
pack
Structure-Packing Pragmas - Using the GNU Compiler Collection (GCC)
Hopefully something there will point you in the right direction.
I assume most compilers do. But I am pretty sure you can cook up a way to investigate this.
First idea that comes to my mind, is to try creating a struct which obviously would be padded if padding is done to 4 byte boundary. For example:
typedef struct SPadTest
{
char byte1;
char byte2;
unsigned long long int eightbytesPropably;
char yetAnotherByte;
}SPadTest;
Now sizeof(SPadTest) should give you an idea.
and
should give you a strong hint.Code:unsigned int i; SPadTest test; memset(&test,0,sizeof(SPadTest)); test.byte1=0xFF; test.byte2=0xFF; test.eightbytesPropably=0xFFFFFFFFFFFFFFFF; test.yetAnotherByte=0xFF; for(i=0;i<sizeof(SPadTest);i++) { printf("0x%x",*(((char *)&test)+i)); }
There's also #pragma(s) to set desired padding, like #pragma pack() - but that probably is compiler dependant too.
So if you need to ensure padding with compilers you do not own, then I guess it is best to try inventing reliable way to detect padding using sizeof() and some test structs. (Or then someone will tell you the macro/exact answer - although I have not heard of such macro - especially any such macro that would reliably work on all compilers )
Obsession to networking and protocols made me cook up these:
NSN - Network Status Notifier
epb - Ethernet Packet Bombardier
T.H.O.N.G.S - Textmode Helper On Network Getting Sniffed
Nibbles - console UDP print listener/filter + something else
Feel free to try, comment and improve =)
Both GCC and VS seem to support #pragma pack, don't know about others.There's also #pragma(s) to set desired padding, like #pragma pack() - but that probably is compiler dependant too.