Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Structures

Reply
Thread Tools

Structures

 
 
ben
Guest
Posts: n/a
 
      03-14-2005
Hi guyz,
I have a small doubt in the following program.Upon
execution,the program shows that size of structure variable is 20
whereas size of its individual elements is 4,4,4,1 and 4.If we add the
size of individual elements we get the total as 17.Then how come the
size of the structure varialble is 20?(The program gives the same ans.
on linux as well as WINDOWS(VC++)).

#include<iostream>
using namespace std;

struct Column
{
char *fieldName;
int fieldType;
void *defaultValue;
void *value;
bool nullFlag;
};

int main()
{
Column a;
a.nullFlag=false;
cout<<sizeof(a)<<endl;
cout<<sizeof(a.fieldName)<<endl;
cout<<sizeof(a.fieldType)<<endl;
cout<<sizeof(a.defaultValue)<<endl;
cout<<sizeof(a.nullFlag)<<endl;
cout<<sizeof(a.value)<<endl;
return 0;
}
Program output:20
4
4
4
1
4
bye,
ben

 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      03-14-2005
ben wrote:

> Hi guyz,
> I have a small doubt in the following program.Upon
> execution,the program shows that size of structure variable is 20
> whereas size of its individual elements is 4,4,4,1 and 4.If we add the
> size of individual elements we get the total as 17.Then how come the
> size of the structure varialble is 20?(The program gives the same ans.
> on linux as well as WINDOWS(VC++)).


The compiler is allowed to put in padding bytes to ensure the members are
properly aligned. On many systems, e.g. a 4 byte integer in memory can only
be accessed if its address is a multiple of 4. Some systems allow accessing
unaligned variables, but at the price of a slower access. So what happens
is that after the bool member, 3 padding bytes are inserted, so that in the
case of an array of Column, the first member of the next array element is
again aligned at a 4 byte boundary.

> #include<iostream>
> using namespace std;
>
> struct Column
> {
> char *fieldName;
> int fieldType;
> void *defaultValue;
> void *value;
> bool nullFlag;
> };
>
> int main()
> {
> Column a;
> a.nullFlag=false;
> cout<<sizeof(a)<<endl;
> cout<<sizeof(a.fieldName)<<endl;
> cout<<sizeof(a.fieldType)<<endl;
> cout<<sizeof(a.defaultValue)<<endl;
> cout<<sizeof(a.nullFlag)<<endl;
> cout<<sizeof(a.value)<<endl;
> return 0;
> }
> Program output:20
> 4
> 4
> 4
> 1
> 4
> bye,
> ben


 
Reply With Quote
 
 
 
 
Thomas Matthews
Guest
Posts: n/a
 
      03-14-2005
ben wrote:

> Hi guyz,
> I have a small doubt in the following program.Upon
> execution,the program shows that size of structure variable is 20
> whereas size of its individual elements is 4,4,4,1 and 4.If we add the
> size of individual elements we get the total as 17.Then how come the
> size of the structure varialble is 20?(The program gives the same ans.
> on linux as well as WINDOWS(VC++)).


In addition to what Rolf said, remember this rule:
The size of a structure may be greater than the sum
of the sizes of the members.

Also note that because of the padding, structures
should not be written in binary to streams, especially
for retrieving at a later date. The better method
is to create binary reading and writing methods
which write out the individual members.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
Reply With Quote
 
ben
Guest
Posts: n/a
 
      03-15-2005
Thanks.This is something i never came across in any C++ book.Thanks
again.

 
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
structures, structures and more structures (questions about nestedstructures) Alfonso Morra C Programming 11 09-24-2005 07:42 PM
unconstrained structures Olaf Petzold VHDL 4 08-30-2005 05:46 PM
Type Casting IPv4 and IPv6 structures to Generic Structures tweak C Programming 14 06-11-2004 02:43 PM
sharing complex data structures between threads in mod_perl2 Dennis Gavrilov Perl 1 07-24-2003 07:22 AM
Again the synthetize problems, structures Pedro Claro VHDL 2 07-16-2003 04:52 PM



Advertisments