3

Supposedly C# structs have some performance benefits over classes for lightweight data objects because they're stored entirely on the stack instead of allocating heap memory.

If one of the members of that struct is an int[] or an array of structs, what effect does that have on allocation and any other performance benefits I might get out of it?

Example:

struct A { int foo; int bar; } struct B { int[] foos; A[] bars; } 

How would struct A differ from struct B in performance, memory usage, etc?

3
  • 1
    "Supposedly C# structs have some performance benefits over classes for lightweight data objects because they're stored entirely on the stack instead of allocating heap memory." - Yes, in some circumstances, value type semantics are preferable. Before worrying about your question here I would go read up on value type vs reference types. Once you do you will be able to answer this yourself :) Commented Jun 27, 2012 at 1:36
  • @Ed S. I have a decent understanding and have a preliminary answer. I'm just putting it out there to see if there's anything I missed in my research. Commented Jun 27, 2012 at 1:44
  • 2
    @CodeFusionMobile: Ok, but there are many important details here. For example, a struct member of a type which is allocated on the managed heap is still in fact allocated on the heap. If you haven't read this already, a good read on the subject here Commented Jun 27, 2012 at 1:49

2 Answers 2

2

Since the size is not part of the type, arrays must be dynamically allocated. So your struct just contains a pointer to the array.

The struct itself is stack allocated* but the array contents are still on the heap.

*Usually. There are some cases where value types are heap allocated, such as being an outer variable used in an anonymous method. And of course, if it's part of an object that is heap allocated, the struct is allocated inline on the heap. There are probably some other corner cases, but this isn't really important.

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

4 Comments

"The struct itself is stack allocated" -- careful here. That's not (always) true and will probably garner some dirty looks from the pedants around here (including me).
It's also worth noting that the initial state of the struct contains two null references.
@Kirk Sorry, I was just going by Microsoft's site When you call the New operator on a class, it will be allocated on the heap. However, when you instantiate a struct, it gets created on the stack.
@Antimony, it's not so simple; as Ed noted above, check out this article by Eric Lippert. It's a fun read, and really goes into depth about this topic.
0

Arrays within a struct will not defeat the performance benefit from stack vs heap allocation of the struct. If you actually had a benefit ;). The array within the struct will be allocated on the heap when you create the array-instance.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.