3

How does C# handle arrays of struct - do I need to allocate each array element (as if it was an array of class objects)?

Example:

public struct RGBA { public byte red, green, blue, alpha; }

public RGBA [] colorBuffer = new RGBA [1024*1024];

Now is colorBuffer an array of pointers to RGBA objects, and do I have to allocate them, e.g. like this:

for (int i = 0; i < colorBuffer.Length; i++)
    colorBuffer [i] = new RGBA ();

or does colorBuffer point to a memory chunk of 4 MB in size, containing 1 MB RGBA structs?

3
  • 1
    I had thought of that, but I decided to ask instead. No need to waste your time on questions you don't want to answer. ;) Commented Aug 16, 2011 at 13:43
  • Do not use arrays of structs, do you know what is boxing/unboxing? Use List<T> Commented Aug 16, 2011 at 14:23
  • @sll, you don't know what boxing/unboxing is. There will be no boxing/unboxing with a strongly typed array (as in the question)! Commented Mar 25, 2012 at 1:15

4 Answers 4

8

The colorBuffer variable value will be a reference to the array object. The array object itself will be a single object, 4MB in size (4 bytes * 1024 * 1024). The array is a reference type, but each element is a value type. The element value is the RGBA value - it's not a pointer.

The array will be initialized to default(RGBA) for each element automatically, do you don't need to perform your own initialization.

If you do:

RGBA[] other = colorBuffer;

that's just copying a reference - the two variables now refer to the same array.

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

3 Comments

Thanks for the insight. That leads me to the next question: I am having a const array of struct, and the structs contained the array need to be initialized to various values. The only way I found to write this down was to allocate each struct in the array:
@karx11erx: There's really no such thing as a "const array" - but if you mean you've got a static readonly variable, I suggest you write a method to create the array... that'll be easier to manage. LINQ may work for you too.
Actually I am just trying to port a simple lookup table from C++ ... btw, yeah, 4 MB memory with 1 MB elements.
1

Structs are always value-type. If you create array of 1024*1024 items, the required memory is (1024*1024)*(1+1+1+1) + some_overhead_of_array.

Comments

1

Arrays in C# are reference types, even though it's underlying type my be value type.

3 Comments

Your heading says this. "Array of struct - array elements are reference or value type?" Is that not also a question?
You said "arrays are reference types", but I asked about the "array elements". That's quite a difference, and I made sure to express myself properly in the question heading.
@karx11erx, yeah true. Sorry I get ahead of myself sometimes. Esp when Mr Skeet is awake. Oh btw if you get a chance read C# in Depth, it goes through Reference and Values types very nicely. Have to give the man rep! ;)
1

The array elements in this case are value types. They do not need to be created using new. For example, the following code will compile and execute fine:

void Main()
{
    var colorBuffer = new RGBA [10];
    for (int i = 0; i < colorBuffer.Length; i++)
    {
        colorBuffer[i].red = (byte)i;
        Console.WriteLine(colorBuffer[i].red);
    }
}

struct RGBA { public byte red, green, blue, alpha; }

Comments

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.