# Structures

Discussion in 'C++' started by ben, Mar 14, 2005.

1. ### benGuest

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

ben, Mar 14, 2005

2. ### Rolf MagnusGuest

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

Rolf Magnus, Mar 14, 2005

3. ### Thomas MatthewsGuest

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

Thomas Matthews, Mar 14, 2005
4. ### benGuest

Thanks.This is something i never came across in any C++ book.Thanks
again.

ben, Mar 15, 2005