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 3 2664
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
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
Thanks.This is something i never came across in any C++ book.Thanks
again. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: kazack |
last post by:
Hi all it's me again with another question as I got further in my book. The
chapter I am in covers structres, abstract data and classes. I only read
through to the end of the coverage on...
|
by: Peter Seaman |
last post by:
I understand that structures are value types and arrays and classes are
reference types. But what about arrays as members of structures i.e. as in
C
struct x
{
int n;
int a;
}
|
by: Ken Allen |
last post by:
OK, I admit that I have been programming since before C++ was invented,
and I have developed more than my share of assembly language systems,
and even contributed to operating system and compiler...
|
by: John Dann |
last post by:
I'm trying to read some binary data from a file created by another
program. I know the binary file format but can't change or control the
format. The binary data is organised such that it should...
|
by: pmclinn |
last post by:
I've noticed that many programmers use classes to store data about such
things like:
Class Customers
.....Phone
....ID
....Address
End Class....
|
by: thomasfarrow |
last post by:
At work, our development team has a development standards document that
insists Structures should never be used. I'm looking to change this
standard but need a suitable argument in order to make...
|
by: efrat |
last post by:
Hello,
I'm planning to use Python in order to teach a DSA (data structures
and algorithms) course in an academic institute. If you could help out
with the following questions, I'd sure...
|
by: svata |
last post by:
Hello,
I wonder how to resize such array of structures using realloc()?
#include <stdio.h>
#include <stdlib.h>
#define FIRST 7
typedef struct {
char *name;
|
by: cleanrabbit |
last post by:
Hello!
I hate having to do this, because im almost certain there is someone in the world that has come across this problem and i just havent found their solution yet, so i do appologise if this...
|
by: Bob Altman |
last post by:
Hi all,
I have a structure that includes a constructor. I want to add a bunch of these
structures to an STL map (whose index is an int). If I define the map like
this:
map<int,...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
| |