Array of structs

Started by
3 comments, last by Xanather 11 years, 5 months ago
A struct in C# is a value type.

For a 2D tile based game I am developing I just made a 2D array of tile.cs structs, this tile struct contains basic tile information (i.e. tile type, draw frame, etc...). I thought making a array of structs for tile data would be better than creating a array of class types (as structs do not need a memory reference and are allocated on the stack). I think using structs did indeed lower memory usage aswell.

The problem I have is, structs can act very differently to classes sometimes, and it just confuses me. Say I have a x int value in a struct, and I want to incrementt that x value by 1. Typing struct[index].x++ just does not let me? Why??

I have a feeling that it may be because of the following quote from MSDN on structs:

Structs are copied on assignment. When a struct is assigned to a new variable, all the data is copied, and any modification to the new copy does not change the data for the original copy. This is important to remember when working with collections of value types such as Dictionary<string, myStruct>.[/quote]

What do other programmers think? Use structs or classes in this case. The size of the array that I am making is 16000x4000, so very large.

All replies are appriciated, thanks.
Xanather.
Advertisement
How about a class which wraps an array of your structs? Then you could pass that to methods by reference and you could access the underlying struct array. There shouldn't be much difference using structs or classes for your tiles, as long as the classes are simple (no virtual methods, etc..) but I don't know much about how classes work in C#. Perhaps there is a lot of overhead in creating and managing a class relative to a simple struct.

Is it a bottleneck?

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

I never use structs. I find this useful: http://stackoverflow.com/questions/521298/when-to-use-struct-in-c

In the linked post, the following is said:


Do not define a structure unless the type has all of the following characteristics:

  1. It logically represents a single value, similar to primitive types (integer, double, and so on).
  2. It has an instance size smaller than 16 bytes.
  3. It is immutable.
  4. It will not have to be boxed frequently.

[/quote]

I agree with these conditions.

#3 is a good point as you found in your "array[index].x++;" attempt. To modify a struct, you (in effect) MUST modify the entire struct, except in the very rare case where your struct instance is a local variable.

#1 follows from #3. If you must treat something as immutable, it should be treated as a single value.

The other two are performance considerations. I typically design applications based on behavior considerations first and only address performance if it bothers me, so I don't have personal experience fighting with them. I've *never* needed to use structs to address performance issues, and misuse of structs can actually hurt performance compared to classes.


Another "feature" of structs that I personally find to get in my way is that (without boxing them) they cannot be "null". You can use the nullable-type modifier, but that forces the variable to be boxed/unboxed.


You mention that you want a 16000x4000 array of these items, whatever they may be. I would personally either:

- Use a class (Even Terraria, with its huge 2D array of tiles, uses a class as its Tile type)
or
- Use struct-of-arrays (a separate array for each 'member' of your original struct). This might be good or bad depending on your situation, but at the very least you will be able to modify each individual "field" independently and without major headaches.
You need to pass any variable you want a function to change in C# with a ref tag, this must also be added to the function definition to work. This way you will tell C# to actually pass the parameter by reference instead of by value. See the example below.

class Program
{
public struct POD
{
public int m_value;
}
static void modifyStructsByRef(ref POD[] structs)
{
for (int counter = 0; counter < structs.Length;++counter )
{
structs[counter].m_value++;
}
}
static void modifyStructsByValue(POD[] structs)
{
for (int counter = 0; counter < structs.Length; ++counter)
{
structs[counter].m_value++;
}
}
static void Main(string[] args)
{
POD[] structs = new POD[4];
for (int counter = 0; counter < structs.Length; ++counter)
{
structs[counter].m_value = counter;
}
modifyStructsByValue(structs);
for (int counter = 0; counter < structs.Length; ++counter)
{
Console.WriteLine(structs[counter].m_value);
}
modifyStructsByRef(ref structs);
for (int counter = 0; counter < structs.Length; ++counter)
{
Console.WriteLine(structs[counter].m_value);
}
Console.ReadKey();
}

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

Thanks for all the posts.
@Nypyren - that link was very helpful, as so was your post :). I dont know why my C# 4.0 book didnt go into details about this. After learning about such things I am sure as heck just going to use a class lol.

Thanks again,
Xanather.

This topic is closed to new replies.

Advertisement