473,385 Members | 1,370 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Length of a struct

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
Apr 28 '06 #1
2 1503
Padding.
public UInt16 Sekunde; //padded to 32 bits
public UInt32 BehälterId; //32 bits
public UInt32 WannenNr;//32 bits
public UInt16 Station; //padded to 32 bits
you can change padding http://www.vsj.co.uk/articles/display.asp?id=501

"Stephan" <St*****@discussions.microsoft.com> wrote in message
news:65**********************************@microsof t.com... 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

Apr 28 '06 #2
Stephan wrote:
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.
Apr 28 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Panchal V | last post by:
I want to access a variable length record in C, the format is as follows : +---+---+-----------+ | A | L | D A T A | +---+---+-----------+ A - Some Data (1 BYTE) L - Length the Data that...
10
by: Adam Warner | last post by:
Hi all, With this structure that records the length of an array of pointers as its first member: struct array { ptrdiff_t length; void *ptr; };
5
by: Tiger | last post by:
Hi, I try to send a packet with raw socket but I have an error with ethereal sniffer on windows xp. I can't find any solution on the net. :( Could anybody help with that problem? my code :...
3
by: Bill Pursell | last post by:
I have a program that does most of its work traversing a bunch of lists. The lists contain a void *, and I spent some time today replacing the void *'s with a copy of the data at the end of the...
8
by: Andrew Smallshaw | last post by:
I'm working on a data structure that began life as a skip list derivative, but has evolved to the point that it now only has a passing resemblance to them. Each node of this structure has a few...
1
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.