Re: querry related to structure padding



Jack Klein wrote:
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.

Dumb question:

A "byte" need not be 8 bits in size, though it is under PCs right?

.



Relevant Pages

  • Re: It works without stdio.h !!
    ... most C compilers will assume that a function that has no prototype is an ... extern int, and since you only specify functions that live in the C library, an unresolved ... never declare a variable of type char unless you well and truly believe you need to be ... strcpy, strcat, sprintf etc. with it. ...
    (microsoft.public.vc.mfc)
  • Re: K&R-Style Function Declarations: Good or Bad?
    ... > short parameters getting promoted to int. ... all pre-ANSI compilers widened char and short ... declarations -- some compilers converted the widened types back to the ...
    (comp.lang.c)
  • Re: querry related to structure padding
    ... char B; ... unsigned char C; ...
    (comp.lang.c)
  • Re: APPEND Option not recognized
    ... >> I remember when this error popped up I was trying to convince some very ... > at least some compilers where that is true. ... > C to assume char is 8 bits. ... In the first Cray C compiler, short, int, and long were all 64 ...
    (comp.lang.fortran)
  • Re: querry related to structure padding
    ... char B; ... declare your struct like this: ...
    (comp.lang.c)