Re: querry related to structure padding
- From: Jack Klein <jackklein@xxxxxxxxxxx>
- Date: Sat, 18 Mar 2006 22:11:17 -0600
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
.
- Follow-Ups:
- Re: querry related to structure padding
- From: santosh
- Re: querry related to structure padding
- References:
- querry related to structure padding
- From: Lalatendu Das
- querry related to structure padding
- Prev by Date: Re: querry related to structure padding
- Next by Date: Re: querry related to structure padding
- Previous by thread: Re: querry related to structure padding
- Next by thread: Re: querry related to structure padding
- Index(es):
Relevant Pages
|