943,587 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 2113
  • C# RSS
Jun 26th, 2009
0

Fixed size structure in C#

Expand Post »
Hello,
How can I define fixed size of structure in c#..
e.g.

C# Syntax (Toggle Plain Text)
  1. struct xyz{
  2. char localc[12];
  3. short localshort;
  4. };
  5.  
  6. I had made a c# structure like this
  7. [StructLayout(LayoutKind.Sequential, Pack = 2,CharSet=CharSet.Ansi, Size = 14)]
  8. struct xyz{
  9. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 12)]
  10. public char[] localc;
  11. short localshort;
  12. }
  13.  
  14. xyz abc = new xyz();
Here i am not able to get the sizeof abc as 14 ..and got error while mashal.sizeof(abc);
How should i preallocate memory of size of structure ?
I can easily does that in c/c++ by just declaring it .. how to make this possible in c#?
and i can initialise the structure using memset in c .. whats its alternative in c#?

It will be helpful to get the solution
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
niketan is offline Offline
4 posts
since Jul 2008
Jun 26th, 2009
0

Re: Fixed size structure in C#

I am not sure about that, but unsafe could do the trick...
Reputation Points: 10
Solved Threads: 9
Junior Poster
VIeditorlover is offline Offline
137 posts
since Dec 2007
Jun 26th, 2009
0

Re: Fixed size structure in C#

Import - using System.Runtime.InteropServices;
C# Syntax (Toggle Plain Text)
  1. xyz p = new xyz();
  2. MessageBox.Show(Marshal.SizeOf(p).ToString());

Read this http://msdn.microsoft.com/en-us/libr...ya(VS.80).aspx
Last edited by adatapost; Jun 26th, 2009 at 9:08 am.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Jun 26th, 2009
0

Re: Fixed size structure in C#

In .NET the char data type is is 16bit, most other languages it is 8bit. You will also need to watch out for that.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Jun 27th, 2009
0

Re: Fixed size structure in C#

Because of this unmanaged attribute a size of char is 8 bits.

C# Syntax (Toggle Plain Text)
  1. [StructLayout(LayoutKind.Sequential, Pack = 2, CharSet = CharSet.Ansi, Size = 14)]
  2. struct xyz
  3. {
  4. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 12)]
  5. public char[] localc;
  6. short localshort;
  7. }
  8. ...
  9. ...
  10. xyz p = new xyz();
  11. int size=Marshal.SizeOf(p);
  12. ..

So the size of struct is 14 bytes.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: csv read by jet engine throws error 'specified cast is not valid'
Next Thread in C# Forum Timeline: C#.net





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC