0

I need an array of structs (which ARE all unmanaged structs with a fixed size) but apparently visual studio does not like my code.

Basically I NEED something like

fixed page_table tables[1024]; in my struct.

This is the code that makes visual studio throw a fit, is there anyway I can achieve this (and I need it all pre-initialized)

[StructLayout(LayoutKind.Explicit, Pack = 1)]
public unsafe struct page_directory
{
    [FieldOffset(0)]
    public fixed page_table tables[1024];

    [FieldOffset(0x8000)]
    public fixed uint tablesPhysical[1024];

    [FieldOffset(0x9000)]
    public uint physicalAddr;
}

[StructLayout(LayoutKind.Explicit, Pack = 1)]
public unsafe struct page_table
{
    [FieldOffset(0)]
    public fixed page pages[1024];
}
5
  • Could you clarify "does not like" and "throw a fit"? What happens when you try the code, and how does it differ from what you expect? Do you get any error message, and if so, which? Commented Jan 19, 2013 at 9:14
  • Fixed size buffer type must be one of the following: bool, byte, short, int, long, char, sbyte, ushort, uint, ulong, float or double A fixed sized buffer may not be any type other than those listed. To avoid this error, use another type or do not use a fixed array. Commented Jan 19, 2013 at 9:15
  • But the thing is my struct has a fixed size, its not like I threw a string in there or something..... And int is a struct and somehow C# accepts that..... Commented Jan 19, 2013 at 9:15
  • How will you use this? Can you get away with just using a page* that "just happens" to point to memory big enough for 1024 pages? Commented Jan 19, 2013 at 9:33
  • Yeah thats actually what I am going do.... Commented Jan 19, 2013 at 17:50

1 Answer 1

1

The error message is pretty clear. You can't use any other types than the listed with a fixed buffer.

The error message even gives you the possible solutions, either use one of the allowed types, or don't use a fixed buffer.

If you really need the code that you are trying to use, then you have reached the point where it's simply not possible to do whatever you are trying to do.

Sign up to request clarification or add additional context in comments.

2 Comments

Well the thing I do not get is how it accepts those types because from my understanding none of them are actually hardcoded into the C# language, rather coded in C# and exist somewhere in MSCORLIB.DLL...
@user1454902: The fixed buffer is restricted to those types, but the documentation doesn't say specifically why. Those types are actually hardcoded in the language, but they are also defined in the framework.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.