469,910 Members | 1,486 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,910 developers. It's quick & easy.

pinning member of struct -> entire struct pinned?

If I pin a member of a garbage collected structure, can I safely assume
that the entire struct is pinned?

I use something like that: (new whidbey syntax, but that shouldn't make
a difference):

[StructLayout(LayoutKind::Sequential)]
public ref struct MyStruct {
Int32 memberA;
Int16 memberB;
Int32 memberC;
}
....

MyStruct^ struct = gcnew MyStruct();
void* pData = somedata_somwhere;

//pData points to a memory location,
//that holds data not only about
//memberA but also memberB and memberC
pin_ptr<int> pMemberA = &struct->membera;
memcpy((void*)pMemberA, pData, sizeofdata);
pMemberA = nullptr;
it works but is it correct?
Would it also be correct if i hadn't specified LayoutKind::Sequential?

Thanks
Ben
Nov 17 '05 #1
3 1075
> Would it also be correct if i hadn't specified LayoutKind::Sequential?
Replying to myself:
Well, since memberA is not guranteed to be the first member in memory, I
suppose it wouldn't be correct to use a pointer to memberA as a pointer
to the struct.
Still, could the GC potentially shift other members around if only one
member is pinned and the LayoutKind is not specified?

I know that when you pin a member of an array, the entire array gets
pinned.
Nov 17 '05 #2
No, pinning any member will pin the entire object. That is a hard guarantee
that the CLI spec makes.

Practically in the current CLR implementation, different parts do not move
independently or get rearranged at all. Future implementations might in fact
start doing this, so you were correct to think of this possibility.

Ronald Laeremans
Visual C++ team

"Ben Schwehn" <b.*******@gmx.net> wrote in message
news:2k************@uni-berlin.de...
Would it also be correct if i hadn't specified LayoutKind::Sequential?

Replying to myself:
Well, since memberA is not guranteed to be the first member in memory, I
suppose it wouldn't be correct to use a pointer to memberA as a pointer to
the struct.
Still, could the GC potentially shift other members around if only one
member is pinned and the LayoutKind is not specified?

I know that when you pin a member of an array, the entire array gets
pinned.

Nov 17 '05 #3
> No, pinning any member will pin the entire object. That is a hard guarantee
that the CLI spec makes.

Thanks!

Ben
Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Wenjie | last post: by
2 posts views Thread by Chris Finlayson | last post: by
3 posts views Thread by J | last post: by
4 posts views Thread by Sai Kit Tong | last post: by
1 post views Thread by demofo | last post: by
reply views Thread by Bjoern Jonsson via .NET 247 | last post: by
9 posts views Thread by Brian Victor | last post: by
4 posts views Thread by =?Utf-8?B?VGhlTWFkSGF0dGVy?= | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.