By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,501 Members | 1,690 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,501 IT Pros & Developers. It's quick & easy.

Structures

P: n/a
ben
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

Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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


Jul 23 '05 #2

P: n/a
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
Jul 23 '05 #3

P: n/a
ben
Thanks.This is something i never came across in any C++ book.Thanks
again.

Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.