471,605 Members | 1,595 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Unsafe pointers

Hi,

I try to compile the following code (Visual Studio .NET 2005) :

....
somedata = new Byte[1024];

unsafe
{
fixed ( byte *pData = somedata)
{
MY_STRUCT1* ptr = (MY_STRUCT1*)pData;
}
...

, but I get the following error mesasge from compiler :

Error 38 Cannot take the address of, get the size of, or declare a
pointer to a managed type MY_STRUCT1 ...

on the line with the assignment of the ptr pointer.

Here is how MY_STRUCT1 looks like :

[Serializable]
[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct MY_STRUCT1 {
int par1;
int par2;

[MarshalAs(UnmanagedType.ByValArray, SizeConst=8)]
public byte [] abParamVal;
}

If I dont have the line with the abParamVal byte-array in the structure,
everything is fine for the compiler.

Can someone please explain why this does not work ?

Thanks in advance.
Abra

*** Sent via Developersdex http://www.developersdex.com ***
Feb 8 '06 #1
2 1512
Abra,

The reason this does not work is because the array is not embedded in
the structure, so it would treat the four bytes at that address location as
a pointer, which isn't really what you want.

If you are using C#, you should be able to do this:

[Serializable]
[StructLayout(LayoutKind.Sequential, Pack=1)]
public unsafe struct MY_STRUCT1 {
int par1;
int par2;

[MarshalAs(UnmanagedType.ByValArray, SizeConst=8)]
public fixed byte abParamVal[8];
}

And it ^should^ allow you to cast the byte array to the structure.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Abra" <no****@devdex.com> wrote in message
news:Oq**************@tk2msftngp13.phx.gbl...
Hi,

I try to compile the following code (Visual Studio .NET 2005) :

...
somedata = new Byte[1024];

unsafe
{
fixed ( byte *pData = somedata)
{
MY_STRUCT1* ptr = (MY_STRUCT1*)pData;
}
..

, but I get the following error mesasge from compiler :

Error 38 Cannot take the address of, get the size of, or declare a
pointer to a managed type MY_STRUCT1 ...

on the line with the assignment of the ptr pointer.

Here is how MY_STRUCT1 looks like :

[Serializable]
[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct MY_STRUCT1 {
int par1;
int par2;

[MarshalAs(UnmanagedType.ByValArray, SizeConst=8)]
public byte [] abParamVal;
}

If I dont have the line with the abParamVal byte-array in the structure,
everything is fine for the compiler.

Can someone please explain why this does not work ?

Thanks in advance.
Abra

*** Sent via Developersdex http://www.developersdex.com ***

Feb 8 '06 #2
>Can someone please explain why this does not work ?

The compiler pretty much told you why. You can only use pointers to
primitive value types and structs made up of such types. You can't use
any reference types, including arrays.

Since you're using C# 2.0 you can use the fixed array syntax instead

public unsafe struct MY_STRUCT1 {
int par1;
int par2;
fixed byte abParamVal[8];
}
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Feb 8 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Elidel | last post: by
reply views Thread by Wayne Gibson | last post: by
1 post views Thread by Francois Vanderseypen | last post: by
17 posts views Thread by Bradley1234 | last post: by
4 posts views Thread by CodeTyro | last post: by
1 post views Thread by Z | last post: by
reply views Thread by colin | last post: by
1 post views Thread by XIAOLAOHU | last post: by

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.