Length of a struct

G

Guest

Hello NG,

I have two structs.
The only differnce is the order of the members:


public struct DataStruct
{
public UInt16 Sekunde;
public UInt32 BehälterId;
public UInt32 WannenNr;
public UInt16 Station;
};

public struct DataStruct
{
public UInt32 BehälterId;
public UInt32 WannenNr;
public UInt16 Station;
public UInt16 Sekunde;
};


If I use SizeOf for the struct it is 16 forthe first and 12 for the second
struct.
The right value should by 12.

If I change the order, why do I get another length ?

regards
Stephan
 
S

Sericinus hunter

Stephan said:
Hello NG,

I have two structs.
The only differnce is the order of the members:

public struct DataStruct
{
public UInt16 Sekunde;
public UInt32 BehälterId;
public UInt32 WannenNr;
public UInt16 Station;
};

public struct DataStruct
{
public UInt32 BehälterId;
public UInt32 WannenNr;
public UInt16 Station;
public UInt16 Sekunde;
};

If I use SizeOf for the struct it is 16 forthe first and 12 for the second
struct. The right value should by 12.

If I change the order, why do I get another length ?

Suspiciously looks like memory alignment to me. Each field in the struct
is multiple of four bytes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top