Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Structure Query....

Reply
Thread Tools

Structure Query....

 
 
Sachin
Guest
Posts: n/a
 
      09-08-2004
typdef struct
{
int i;
char ch;
}str;
str str_var;
char x, y;
main()
{
//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?
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??

Thanks in advance...
Regds
Sachin
 
Reply With Quote
 
 
 
 
Jens.Toerring@physik.fu-berlin.de
Guest
Posts: n/a
 
      09-08-2004
Sachin <(E-Mail Removed)> 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 ___ http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de
\__________________________ http://www.toerring.de
 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      09-08-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (Sachin) writes:

> typdef struct
> {
> int i;
> char ch;
> }str;
> str str_var;
> char x, y;
> main()
> {
> //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?


No. Then the following:
memset (&str_var, 0, sizeof str_var);
would clear x and y as well as str_var.
--
"A lesson for us all: Even in trivia there are traps."
--Eric Sosman
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between c structure and c++ structure raghunandan_1081@yahoo.com C++ 9 11-11-2011 07:34 AM
How to read a Structure from a matlab file to a structure in Vc++ 2003 meisterbartsch C++ 2 06-12-2007 08:47 PM
Memory allocation in Structure to Structure pra_ramli@rediffmail.com C++ 2 03-09-2006 05:51 AM
Copy String structure "A" to string structure "B" Leo Nunez C Programming 3 02-09-2005 05:14 AM
Pointers to structure and array of structure. Excluded_Middle C Programming 4 10-26-2004 05:39 AM



Advertisments