Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Array of structs


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Xanather   Members   -  Reputation: 712

Like
0Likes
Like

Posted 14 November 2012 - 07:19 PM

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>.


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.

Sponsor:

#2 Bacterius   Crossbones+   -  Reputation: 9068

Like
0Likes
Like

Posted 14 November 2012 - 09:40 PM

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?

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#3 Nypyren   Crossbones+   -  Reputation: 4499

Like
0Likes
Like

Posted 14 November 2012 - 10:15 PM

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:

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


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.

Edited by Nypyren, 14 November 2012 - 10:31 PM.


#4 NightCreature83   Crossbones+   -  Reputation: 2934

Like
0Likes
Like

Posted 15 November 2012 - 03:21 AM

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, Mad Max

#5 Xanather   Members   -  Reputation: 712

Like
0Likes
Like

Posted 15 November 2012 - 03:54 AM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS