dbTalk Databases Forums  

OT:C++ doubt

comp.databases.object comp.databases.object


Discuss OT:C++ doubt in the comp.databases.object forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
ben
 
Posts: n/a

Default OT:C++ doubt - 03-14-2005 , 07:27 AM






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


Reply With Quote
  #2  
Old   
Paul Chapman
 
Posts: n/a

Default Re: OT:C++ doubt - 03-14-2005 , 07:44 AM






Ben,

Quote:
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++)).
C++ rounds up the size of the structure to an integer number of 32-bit
words.

Cheers, Paul




Reply With Quote
Reply




Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.