IntPtr arithmetics

Posted by Luca on Stack Overflow See other posts from Stack Overflow or by Luca
Published on 2009-08-23T14:17:04Z Indexed on 2010/03/12 21:37 UTC
Read the original article Hit count: 270

Filed under:
|
|

I tried to allocate an array of structs in this way:

struct T {
    int a; int b;
}

data = Marshal.AllocHGlobal(count*Marshal.SizeOf(typeof(T));
...

I'd like to access to allocated data "binding" a struct to each element in array allocated with AllocHGlobal... something like this

T v;
v = (T)Marshal.PtrToStructure(data+1, typeof(T));

but i don't find any convenient way... why IntPtr lack of arithmetics? How can I workaround this is a "safe" way?

Someone could confirm that PtrToStructure function copy data into the struct variable? In other words, modifing the struct reflect modifications in the structure array data, or not?

Definitely, I want to operate on data pointed by an IntPtr using struct, without copying data each time, avoiding unsafe code.

Thank all!

© Stack Overflow or respective owner

Related posts about c#

Related posts about intptr