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 7908683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:03:53+00:00 2026-06-03T12:03:53+00:00

i am new to stackoverflow and i would have a question about C# structs

  • 0

i am new to stackoverflow and i would have a question about C# structs and their layout.

Let’s assume following structs:

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public unsafe struct Link
{
    // some primitive data (2 integers for example)
}

[StructLayout(LayoutKind.Explicit, Pack = 1)]
public unsafe struct Node
{
    [FieldOffset(0)]
    public int LinkCount;
    [FieldOffset(4)]
    public Link* Links;
    [FieldOffset(4)]
    private fixed byte _linksData[10 * sizeof(Link)];
}

The reason for that is that i need a blittable type for IO-Performance.
I have to deal with very large (and sparse a maximum of 10 links per node) graphs which have several GB in size.
The graph is represented as an array of Node-structs.
With the setup like above i was hoping of being able to read for example a hundred MB from the graph file into a byte-pointer (which of course points to a byte-buffer) and cast it to a pointer of type Node* which yields very good performance.
At first my Node-struct just had 10 separate variables of type Link (Link0, …, Link10) which worked just fine. But it would be nice to make this configurable at compile-time, which lead to the above Node-struct.

I was hoping, that Links would just point to the same memory location as _linksData since it has the same FieldOffset.
But actually the Links pointer always is a null pointer.

So my question is:
Is there a way that Links points to the same memory location as _linksData or is there another way to have a fixed sized array of structs embedded into another struct.

Thanks for every answer in advance – Markus

After reading Ben Voigt’s post I tryed something similar without the need of changing the struct to a class. The following is how it works for me:

[StructLayout(LayoutKind.Explicit, Pack = 1)]
public unsafe struct Node
{
    [FieldOffset(0)]
    public int LinkCount;
    [FieldOffset(4)]
    private fixed byte _linksData[10 * sizeof(Link)];

    public Link* GetLinks()
    {
       fixed(byte* pLinksData = _linksData)
       {
          return (Link*)pLinksData;
       }
    }
}
  • 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-06-03T12:03:54+00:00Added an answer on June 3, 2026 at 12:03 pm

    I guess that you’re not actually trying to store a pointer, just have a correctly-typed way to access the 10 elements. How about:

    [StructLayout(LayoutKind.Explicit, Pack = 1)]
    public unsafe struct Node
    {
        [FieldOffset(0)]
        public int LinkCount;
        [FieldOffset(4)]
        private fixed byte _linksData[10 * sizeof(Link)];
    
        public Link* Links { get { return _linksData; } };
    }
    

    No, wait, .NET supports interior pointers but C# doesn’t, so that won’t work. You can only have a pointer into a .NET object if you’ve pinned it or placed it on the stack, and we don’t know if that’s the case here.

    🙁

    Full-on wrapper time:

    public class LinkCollection
    {
        Node peer;
        public LinkCollection(Node node) { peer = node; }
        void CheckIndex(int index) { if (index < 0 || index >= 10) throw new ArgumentOutOfRangeException(); }
        public Link default[int index] {
            get { CheckIndex(index); return peer.GetLink(index); }
            set { CheckIndex(index); peer.SetLink(index, value); }
        }
    }
    
    [StructLayout(LayoutKind.Explicit, Pack = 1)]
    public unsafe class Node
    {
        [FieldOffset(0)]
        public int LinkCount;
        [FieldOffset(4)]
        private fixed byte _linksData[10 * sizeof(Link)];
    
        unsafe Link GetLink(int index) { fixed( Link* plink = (Link*)&_linksData[0] ) return plink[index]; }
        unsafe void SetLink(int index, Link newvalue) { fixed( Link* plink = (Link*)&linksData[0] ) plink[index] = newvalue; }
        public LinkCollection Links { get { return new LinkCollection(this); } };
    }
    

    Note that I had to change Node to a class… p/invoke should still act pretty much the same though.

    If you don’t want to do that, extension methods might be an answer.

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

Sidebar

Related Questions

I'm new to Stack Overflow, but I have a question about writing some Java
I have a question about templates. I would like to have a templated class
I'm new here to stackoverflow, so bear with me. I have a book that
I'm new to programming and stackoverflow and have decided to start by learning objective
I have a question about storing site configuration data. We have a platform for
Recently, I asked (and answered) a question on StackOverflow about why a unit test
I'm a little confused about the concept of services. Let's assume i want to
I have seen some code for working with AD in this stackoverflow question I
I'm new to StackOverflow, so please let me know if there is a better
We have all seen many question on StackOverflow that are founded upon the idea

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.