Re: Structure Query....
Jens.Toerring_at_physik.fu-berlin.de
Date: 09/08/04
- Next message: VJ: "C program to flowchart"
- Previous message: P. Hari Krishna: "Creating a va_list"
- In reply to: Sachin: "Structure Query...."
- Next in thread: Ben Pfaff: "Re: Structure Query...."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 8 Sep 2004 09:50:04 GMT
Sachin <sachin.cas@gmail.com> wrote:
> typdef struct
> {
> int i;
> char ch;
> }str;
> str str_var;
> char x, y;
> main()
int main( void )
> {
> //do nothing
> }
> In the above piece of code the structure needs a padding of 3 more
> bytes (the total size of the structure variable being 8 bytes). Now
> my question is, wont the compiler append x & y to the end of structure
> and adds just 1 byte as the padding?
First of all, the compiler is entitled to add as much padding as
it wants, so you should never rely on a certain value. Second,
you can't know if the compiler will put x and y at a certain place
in relation to the structure, it could put the before or after the
structure or somewhere completely different. Third, even if both
these variable would come directly after the structure in memory
the structure would still have its full padding - if different
padding would be used dependening on circumstances the value of
'sizeof( struct str )' wouldn't be welldefined. And imagine what
would happen then if you define another such structure and do
memcpy( &str_var, &another_str_struct, sizeof( struct str ) );
> In my case I use MSVC compiler. When I checked the size of obj in the
> map file the obj size was 12 bytes....shouldnt it be 8 bytes ideally??
No idea what "size of obj in the map file" is, but you can simply
get the size with the sizeof operator. But the compiler is still
free to add as much padding as it likes to (or thinks necessary).
If that's a different value from what you think is necessary you
have to discuss that with the people that wrote the compiler, it
is nothing the C standard specifies.
Regards, Jens
-- \ Jens Thoms Toerring ___ Jens.Toerring@physik.fu-berlin.de \__________________________ http://www.toerring.de
- Next message: VJ: "C program to flowchart"
- Previous message: P. Hari Krishna: "Creating a va_list"
- In reply to: Sachin: "Structure Query...."
- Next in thread: Ben Pfaff: "Re: Structure Query...."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|