Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7177405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:42:26+00:00 2026-05-28T16:42:26+00:00

I have some code intended to get a struct from a byte array: public

  • 0

I have some code intended to get a struct from a byte array:

    public static T GetValue<T>(byte[] data, int start) where T : struct
    {
        T d = default(T);
        int elementsize = Marshal.SizeOf(typeof(T));

        GCHandle sh = GCHandle.Alloc(d, GCHandleType.Pinned);
        Marshal.Copy(data, start, sh.AddrOfPinnedObject(), elementsize);
        sh.Free();

        return d;
    }

However, the structure d is never modified, and always returns its default value.

I have looked up the ‘correct’ way to do this and am using that instead, but am still curious, as I cannot see why the above should not work.

Its as simple as can be: allocate some memory, d, get a pointer to it, copy some bytes into the memory pointed at by this, return.
Not only that, but when I use similar code but with d being an array of T, it works fine.
Unless sh.AddrOfPinnedObject() isn’t really pointing to d, but then what is the point of it?

Can anyone tell me why the above does not work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T16:42:26+00:00Added an answer on May 28, 2026 at 4:42 pm

    Warning implementation detail alert, this may not be true in future versions of .Net.

    structs are value types and are (generally) stored on the stack (*) , not on the heap. An address of a struct is meaningless since they are passed by value, not by reference. The array of struct is a reference type, that is a pointer to memory on the heap, so the address in memory is perfectly valid.

    The point of AddrOfPinnedObject is to get the address of an object thats memory is pinned, not a struct.

    Additionally, Eric Lippert has written a series of very good blog posts on the subject of reference types and value types.

    (*) Unless:

    1 They are fields on a class
    2 They are boxed
    3 They are “captured variables”
    4 They are in an iterator block

    (nb points 3 and 4 are corollaries of point 1)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code which collects points (consed integers) from a loop which looks
I have some code where I'm returning an array of objects. Here's a simplified
I have some code in a Python except clause that is intended to do
Consider the following code: public abstract class Test1 { public object Data { get;
I have some code for starting a thread on the .NET CF 2.0: ThreadStart
I have some code like this in a winforms app I was writing to
I have some code like this: If key.Equals(search, StringComparison.OrdinalIgnoreCase) Then DoSomething() End If I
I have some code in a javascript file that needs to send queries back
I have some code that gives a user id to a utility that then
I have some code in an IAuthorizationFilter which redirects the user to a login

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.