Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cycle in the struct layout that doesn't exist

This is a simplified version of some of my code:

public struct info
{
    public float a, b;
    public info? c;

    public info(float a, float b, info? c = null)
    {
        this.a = a;
        this.b = b;
        this.c = c;
    }
}

The problem is the error Struct member 'info' causes a cycle in the struct layout. I'm after struct like value type behaviour. I could simulate this using a class and a clone member function, but I don't see why I should need to.

How is this error true? Recursion could perhaps cause construction forever in some similar situations, but I can't think of any way that it could in this case. Below are examples that ought to be fine if the program would compile.

new info(1, 2);
new info(1, 2, null);
new info(1, 2, new info(3, 4));

edit:

The solution I used was to make "info" a class instead of a struct and giving it a member function to returned a copy that I used when passing it. In effect simulating the same behaviour as a struct but with a class.

I also created the following question while looking for an answer.

Value type class definition in C#?

like image 970
alan2here Avatar asked Feb 15 '12 15:02

alan2here


2 Answers

It's not legal to have a struct that contains itself as a member. This is because a struct has fixed size, and it must be at least as large as the sum of the sizes of each of its members. Your type would have to have 8 bytes for the two floats, at least one byte to show whether or not info is null, plus the size of another info. This gives the following inequality:

 size of info >= 4 + 4 + 1 + size of info

This is obviously impossible as it would require your type to be infinitely large.

You have to use a reference type (i.e. class). You can make your class immutable and override Equals and GetHashCode to give value-like behaviour, similar to the String class.

like image 139
Mark Byers Avatar answered Oct 22 '22 02:10

Mark Byers


The reason why this creates a cycle is that Nullable<T> is itself a struct. Because it refers back to info you have a cycle in the layout (info has a field of Nullable<info> and it has a field of info) . It's essentially equivalent to the following

public struct MyNullable<T> {
  public T value;
  public bool hasValue;
}

struct info { 
  public float a, b;
  public MyNullable<info> next;
}
like image 13
JaredPar Avatar answered Oct 22 '22 03:10

JaredPar