Click to See Complete Forum and Search --> : Size of Struct problem???


May 12th, 1999, 12:42 PM
Hi,

Environment : VC++,WinNT

I need to write data in a structure to a file.Iam unable to find the exact size of structure. Please see the folowing code,
struct data
{

int age;
char unit;
CString name;

};


Iam using sizeof operator to find the size of the above structure which gives me 12 bytes. But if u look at the structure fields,

size of int =4 bytes

size of char=1 byes

size of CString =4 bytes.

The Actual total size of structure fields is 9 bytes but the size of operator gives me size of struct as 12 bytes.

How to get the actual size of the structure?

Thanx in advance.
NMNB

May 12th, 1999, 01:03 PM
You are going to have a problem with CString since it is an object of variable size.
Change from CString name to char name[Max_Len], this will defintly give you a fixed size every time

May 12th, 1999, 01:16 PM
Another problem is that you're working with a 32-bit operating system that likes to store things on 32-bit word boundaries. I've found helpful to create structures starting with doubles (8-bytes), then floats & integers (4-bytes) and then characters (1-byte). Less wasted space that way.

May 12th, 1999, 01:20 PM
Thanx for the response. But is there any way to find out the actual size of the data in a structure (I mean elimimating the padding).

Nat

Paul McKenzie
May 12th, 1999, 01:41 PM
Hello, I have a few things to say about the problem that you're having:

1) You have come across something called "structure alignment". By default, structures are aligned on 8 byte boundaries. The extra bytes are from the padding that the compiler places in between the members so that they align correctly. To change this, the #pragma pack(1) can be used to align the members on byte boundaries. Also, the project settings has an option to set the structure alignment.

2) The sizeof() operator should always be used to determine the size of the structure. Adding up the member sizes is *not* the way to do this, as was pointed out in 1).

3) Since you are saving the data to a file, the CString member will be a problem because it is a C++ object with a load of junk that won't make sense to your program when you want to read the data back in. What you want to do is save the string information that the CString object represents. The best way to do this is to declare a char array member instead of the CString.
Example:

#define MAX_NAME_LEN 100
struct {
int age;
char unit
char name[MAX_NAME_LEN+1];
};

You can get into the topic of serialization, but I think that would be overkill for what you want to do.

Hope this helps,

Regards,

Paul McKenzie

May 12th, 1999, 03:25 PM
Thanx for the response. Please let me know how to use #pragma pack(1) and where in project settings the option for setting the alignment of structure.

Thanx.

NMNB

Paul McKenzie
May 12th, 1999, 03:55 PM
For the project settings solution:
Go to Project | Settings...
Select the C++ tab.
From the "Category" drop-down combo, select "Code Generation"
You will see another combo-box stating "struct member alignment". Choose "1 byte".

Here is the #pragma solution:

// Save old struct alignment setting
#pragma pack (push, before_mystruct)

// Set struct alignment setting to 1
#pragma pack(1)

// All of my structures with 1 byte alignment come next
struct {
... This is your structure...
};

// Set struct alignment back to whatever it was before I changed it
#pragma pack (pop, before_mystruct)


Regards,

Paul McKenzie

May 12th, 1999, 04:46 PM
Thank u so much.

NMNB