Fixed size structure in C#

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2008
Posts: 4
Reputation: niketan is an unknown quantity at this point 
Solved Threads: 0
niketan niketan is offline Offline
Newbie Poster

Fixed size structure in C#

 
0
  #1
Jun 26th, 2009
Hello,
How can I define fixed size of structure in c#..
e.g.

  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
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 97
Reputation: VIeditorlover is an unknown quantity at this point 
Solved Threads: 5
VIeditorlover's Avatar
VIeditorlover VIeditorlover is offline Offline
Junior Poster in Training

Re: Fixed size structure in C#

 
0
  #2
Jun 26th, 2009
I am not sure about that, but unsafe could do the trick...
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,612
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 463
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Fixed size structure in C#

 
0
  #3
Jun 26th, 2009
Import - using System.Runtime.InteropServices;
  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.
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,207
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 571
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Fixed size structure in C#

 
0
  #4
Jun 26th, 2009
In .NET the char data type is is 16bit, most other languages it is 8bit. You will also need to watch out for that.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,612
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 463
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Fixed size structure in C#

 
0
  #5
Jun 27th, 2009
Because of this unmanaged attribute a size of char is 8 bits.

  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.
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC