Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / July 2004

Tip: Looking for answers? Try searching our database.

pinning member of struct -> entire struct pinned?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ben Schwehn - 04 Jul 2004 16:14 GMT
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
Ben Schwehn - 04 Jul 2004 16:18 GMT
> 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.
Ronald Laeremans [MSFT] - 06 Jul 2004 16:43 GMT
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

>> Would it also be correct if i hadn't specified LayoutKind::Sequential?
> Replying to myself:
[quoted text clipped - 6 lines]
> I know that when you pin a member of an array, the entire array gets
> pinned.
Ben Schwehn - 06 Jul 2004 17:46 GMT
> No, pinning any member will pin the entire object. That is a hard guarantee
> that the CLI spec makes.

Thanks!

Ben

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.