473,468 Members | 1,352 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

struct size?

Hello,

I have the following struct:

public struct WAVEFORMATEX
{
public UInt16 wFormatTag; //2 bytes
public UInt16 nChannels; //2 bytes
public UInt32 nSamplesPerSec; //4 bytes
public UInt32 nAvgBytesPerSec; //4 bytes
public UInt16 nBlockAlign; //2 bytes
public UInt16 wBitsPerSample; //2 bytes
public UInt16 cbSize; //2 bytes
}

why returns Marshal.SizeOf(typeof(WAVEFORMATEX)) 20??
the size of this struct is only 18 bytes
Nov 15 '05 #1
3 27186
This has to do the alignment overhead of the member fields of your structs.

This overhead equals the largest field in your struct, which is 4 bytes. The total size of your struct will have to be 4 bytes chunks, and 18 div 4 leaves 2. Hence the extra 2 bytes - 5 blocks of 4 bytes.

If you get did get rid of one of the short member fields, you'd notice the structsize would be 16.

Hope that helps.

Wim Hollebrandse
http://www.wimdows.net
http://www.wimdows.com

---
Posted using Wimdows.net Newsgroups - http://www.wimdows.net/newsgroups/
Nov 15 '05 #2
Add

[StructLayout(LayoutKind.Sequential, Pack=1)]

before your struct definition

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://blogs.gotdotnet.com/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
"Dirk Reske" <_F*******@gmx.net> wrote in message
news:eq*************@TK2MSFTNGP09.phx.gbl...
Hello,

I have the following struct:

public struct WAVEFORMATEX
{
public UInt16 wFormatTag; //2 bytes
public UInt16 nChannels; //2 bytes
public UInt32 nSamplesPerSec; //4 bytes
public UInt32 nAvgBytesPerSec; //4 bytes
public UInt16 nBlockAlign; //2 bytes
public UInt16 wBitsPerSample; //2 bytes
public UInt16 cbSize; //2 bytes
}

why returns Marshal.SizeOf(typeof(WAVEFORMATEX)) 20??
the size of this struct is only 18 bytes

Nov 15 '05 #3
I assume you need get your struct into a byte[] and back. If so, here are two options without using the BitConverter and iterating over all your members seperately:

MyStruct1 myStruct1;
myStruct1.myshort = 12;
myStruct1.mylong = 3035;
int size;
byte[] ba;
IntPtr ips;
IntPtr ips2;
//Could use unsafe keyword on the method, but do this way to show where it is needed.
unsafe
{
size = sizeof(MyStruct1);
ba = new byte[size];
byte * pStruct = (byte*)&myStruct1.myshort; //Get a pointer to the first byte.
byte * pStruct2 = (byte*)&myStruct1; //Do they point to same place?
ips = (IntPtr)pStruct; //Cast to an IntPtr for the Marshal.Copy
ips2 = (IntPtr)pStruct2; //method and to see the address.
}
Console.WriteLine("Option1: Copy a Struct to byte[] using Marshal.Copy and back.");
Console.WriteLine("Pointer bp is:"+ ips);
Console.WriteLine("Pointer bp2 is:"+ ips2); //Same as ips? yes.
Marshal.Copy((IntPtr)ips, ba, 0, size); //Copy struct to byte[].

//Now copy byte array back to a new struct and see if same.
MyStruct1 myNewStruct;
myNewStruct.myshort = 0; //Set zero to prove we changed
myNewStruct.mylong = 0; //and to avoid "unassigned" compiler warning.
IntPtr ip;
unsafe
{
ip = (IntPtr)(byte*)&myNewStruct.myshort;
}
Marshal.Copy(ba, 0, ip, size);
Console.WriteLine("Created a new struct and copied bytes to it.");
Console.WriteLine("myNewStruct.myshort:"+myNewStru ct.myshort +
" myNewStruct.mylong:"+myNewStruct.mylong);
Console.WriteLine();

//
//Option#2 - Use Marshal.PtrToStructure and StructureToPtr.
//
ba = new byte[size];
MyStruct1 struct1;
struct1.myshort = 88;
struct1.mylong = 40877;
unsafe
{
//Need fixed in this option because we need a pointer to the managed byte[].
fixed ( byte * fixed_buf = ba )
{
ip = (IntPtr)fixed_buf;
}
}
Marshal.StructureToPtr(struct1, ip, true);
MyStruct1 newStruct = (MyStruct1)Marshal.PtrToStructure(ip, typeof(MyStruct1));
Console.WriteLine("Option2: Copy a struct to byte[] using Marshal.StructureToPtr");
Console.WriteLine("newStruct.myshort:"+newStruct.m yshort+" newStruct.mylong:"+newStruct.mylong);
}

[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct MyStruct1
{
public short myshort;
public long mylong;
}

HTH!

--
William Stacey, MVP

"Dirk Reske" <_F*******@gmx.net> wrote in message news:eq*************@TK2MSFTNGP09.phx.gbl...
Hello,

I have the following struct:

public struct WAVEFORMATEX
{
public UInt16 wFormatTag; //2 bytes
public UInt16 nChannels; //2 bytes
public UInt32 nSamplesPerSec; //4 bytes
public UInt32 nAvgBytesPerSec; //4 bytes
public UInt16 nBlockAlign; //2 bytes
public UInt16 wBitsPerSample; //2 bytes
public UInt16 cbSize; //2 bytes
}

why returns Marshal.SizeOf(typeof(WAVEFORMATEX)) 20??
the size of this struct is only 18 bytes

Nov 15 '05 #4

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

Similar topics

6
by: FTSoJ | last post by:
On a file A.c I define the structure T1 typedef struct { int dummy; int age; char lastname ; char firstname; } T1_user; On a file B.c I define a strcuture T2
7
by: venkatbo | last post by:
Hi folks, Of TurboGers & Django WAF candidates, which one would be easier to use in an environment where the data/content doesn't come an RDBMS, but from other server-side apps... If these are...
2
by: Michael Yanowitz | last post by:
Hello: I am relatively new to Python and this is my first post on this mailing list. I am confused as to why I am getting size differences in the following cases: >>> print...
2
by: guy.gorodish | last post by:
hi, i have a struct in c# that is containing Int32 member and Double member. when i try to get the size of it i get size of 16 bytes, while i was expecting to receive 12. (Int32- 4 bytes, Double...
42
by: Jeff P. Syverson | last post by:
Can anyone tell me the best way to find out the size of a struct, without using the sizeof function? I'm currently thinking of: struct foo s; int size_of_foo = (char *)&s - (char *)s; but is...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.