-
July 7th, 2000, 10:57 AM
#1
Something very strange about struct !!!!
Hi all
I work on a sniffer projet and I need to be able to manipulate struct in memory independantly of the platform. For ex I use the struct :
struct
{char n1;
char n2;
unsigned long n3;
}
to read 3 var of 1,1 and 4 octets !
For this I just use memcpy ! Just so simple !
But When I do : sizeof(struct) is doesn't make 6 octets but 8 !!!!
Why ?? I don't understand why !! When I do a sizeof(n1)+sizzeof(n2) + sizeof(3) I have 6 !!!
Does anyone have an idea ?
-
July 7th, 2000, 11:25 AM
#2
Re: Something very strange about struct !!!!
For performance reasons structures are padded out to (in the case of VC++) multiples of 8 bytes by default. You can override this either by using the commandline switch /Zpn where n is the number of bytes 1,2,4 or 8
Or you can use a preprocessor directive...
#pragma pack(n)
where n is same as for above.
In your case you would use...
#pragma pack(2)
-
July 10th, 2000, 06:28 AM
#3
Re: Something very strange about struct !!!!
Hi
Thanks for the response ! It works ! But can you explain me more ! Why 2 ? And not only 1 !
One must be use just if I have a struct that make 3, 5 ... bytes ?
Where can I find a description of al these pragma ?
Very thanks
-
July 10th, 2000, 08:59 AM
#4
Re: Something very strange about struct !!!!
Well yes, as you see you could have used pack(1) also, but the default is 8, so that says to me "set it to the largest number possible. I don't really know how this is used by the compiler... I have a pretty good idea, but I would not want to show how little I really know, here, in public. :-)
As for the descriptions of the pragmas, I found it on the MSDN CD that came with the compiler. Just do some searchs for "preprocessor pragma" and so on, it should not be to hard to find.
-
July 10th, 2000, 09:47 AM
#5
Re: Something very strange about struct !!!!
Each member of a structure should be correctly alligned for it's particular type.
Memory these days is generally 32bits wide, that is it is much more efficient if you align things on multiple of four byte addresses as the processor can just read all four bytes in one go rather than having to read each byte seperatly e.g. good alignment:
32 bits 32 bits
<--------------------------><--------------------------->
|byte 0|byte 1|byte 2|byte 3|byte 4|byte 5|byte 6|byte 7|
|word 0 |word 1 |word 2 |word 3 |
|dword 0 |dword 1 |
individual bytes can be read from any byte address. word (2byte) values can be read from any even address. double word (4byte) values can be read from any four byte aligned address.
If for example you have a byte followed by a dword in your structure then the compiler will insert three bytes of padding to get the dword correctly aligned.
I believe that PCs do not require this address alignment as the processor can take care of it, but they are much more efficient when it happens.
MS have chosen to use 64 bit (8byte) alignment the default as it will be more efficient in future systems and is no less efficient now
-
July 10th, 2000, 09:56 AM
#6
Re: Something very strange about struct !!!!
Very thanks for your responses !
It's very clear for me now !!
Long life to CodeGuru's web site and CodeGuru's members !
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
|