Re: querry related to structure padding



On 18 Mar 2006 19:34:22 -0800, "Lalatendu Das" <lalatdas@xxxxxxxxx>
wrote in comp.lang.c:

hi let's say i have a structure
struct test {
int A;
char B[5];
int C;
};
this above structure defination always going to take 16 byte in
memeory in whatever manner we align the member variables while
declaring a variable to it .

No, you are completely wrong. It is going to occupy sizeof(struct
test) bytes in memory, no more and no less.

On two different compilers that I use that will be exactly 7 bytes. On
several other compilers that I use that will be 9 bytes, while on
others it will be 10.

because variable 'A' going to take 4 byte then four charachter of

Variable 'A' is going to occupy sizeof(int) bytes. On various
compilers that I use, that varies between 1 and 4 bytes. There may or
may not be padding bytes after 'A'.

array gonna take another 4 bytes but the remaining will take also 4
bytes due to four byte alignment nature of compiler .
and last four byte by integer C .

No, the array 'B' will not take 4 bytes and another 4 bytes, that is 8
bytes in total. Unlike everything else in your post, this is one
thing that is absolute. The array 'B' will occupy exactly 5 bytes, on
your compiler and on every C compiler that ever existed.

There might be padding bytes after 'B' before the start of the next
member. Perhaps that is what you are talking about. Those padding
bytes do not change the size of 'B', an array of 5 char will always
have a size of exactly 5 bytes.

what i want is there any way i can start storing integer C just after
the storing completion of array B's last element i.e. B[4] , so that i
can suppress padding . Any program which make memory manager store in
the above manner is most welcome.

What "memory manager"? There is no "memory manager" in C. The
compiler is allowed to insert padding after any member of a structure
to maintain alignment. On some hardware architectures, incorrect
alignment will cause a hardware trap that will shut down a program.

thanks priorly because i am sure i am gonna get innumerable answer to
it .

C does not define any mechanism for a programmer to override the
compiler's alignment decisions. Your particular compiler might
provide some non-standard mechanism to do this. You need to ask in a
compiler specific support group to find out if this is so, or study
your compiler's documentation. Even if such a non-standard mechanism
is available, it can significantly slow down the program on some
architectures.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
.



Relevant Pages

  • Re: Why #pragma pack() not take effect?
    ... What compiler on which platform are you using? ... maximum alignment (that is, ... You seem to actually want padding instead of packing. ... same structure as on a 64-bit platform such as HP-UX ia64. ...
    (comp.unix.programmer)
  • Re: Isnt it time there was a standard align statement?
    ... C language actually allowed some portable control over data ... /* per member alignment (obviously padding before the first ... fatal with compiler specific switches - and that's no business of the ...
    (comp.lang.c)
  • Re: problem with sizeof
    ... my compiler spits out a warning on the above ... Actually it is because they have different alignment requirements. ... aligned on a multiple of 1). ... the padding is located. ...
    (comp.lang.c)
  • Re: alignment revisited
    ... char -- 8 bits ... 0x0008 XX XX XX XX -- member 'j'. ... If the compiler does not add padding bytes, ...
    (comp.lang.cpp)
  • Re: querry related to structure padding
    ... char B; ... The compiler is still free to insert padding between B and C, ...
    (comp.lang.c)