Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Questions about Structs

MSDN says that a class that would be 16 bytes or less would be better handled as a struct [citation].
Why is that?
Does that mean that if a struct is over 16 bytes it's less efficient than a class or is it the same?
How do you determine if your class is under 16 bytes?
What restricts a struct from acting like a class? (besides disallowing parameterless constructors)

like image 272
Malfist Avatar asked Mar 20 '09 18:03

Malfist


People also ask

What is the purpose of a struct?

Structures (also called structs) are a way to group several related variables into one place. Each variable in the structure is known as a member of the structure. Unlike an array, a structure can contain many different data types (int, float, char, etc.).

What are the elements of a struct called?

Structure elements, called 'members', are arranged sequentially, with the members occupying successive locations in memory.

Can you set a struct equal to another?

Yes, you can assign one instance of a struct to another using a simple assignment statement. In the case of non-pointer or non pointer containing struct members, assignment means copy. In the case of pointer struct members, assignment means pointer will point to the same address of the other pointer.

Can a structure contain pointer to itself?

A structure can have members which point to a structure variable of the same type. These types of structures are called self referential structures and are widely used in dynamic data structures like trees, linked list, etc. The following is a definition of a self referential structure.


2 Answers

There are a couple different answers to this question, and it is a bit subjective, but some reasons I can think of are:

  • structs are value-type, classes are reference type. If you're using 16 bytes for total storage, it's probably not worth it to create memory references (4 to 8 bytes) for each one.
  • When you have really small objects, they can often be pushed onto the IL stack, instead of references to the objects. This can really speed up some code, as you're eliminating a memory dereference on the callee side.
  • There is a bit of extra "fluff" associated with classes in IL, and if your data structure is very small, none of this fluff would be used anyway, so it's just extra junk you don't need.

The most important difference between a struct and a class, though, is that structs are value type and classes are reference type.

like image 182
Mike Avatar answered Oct 05 '22 15:10

Mike


By "efficient", they're probably talking about the amount of memory it takes to represent the class or struct.

On the 32-bit platform, allocating an object requires a minimum of 16 bytes. On a 64-bit platform, the minimum object size is 24 bytes. So, if you're looking at it purely from the amount of memory used, a struct that contains less than 16 bytes of data will be "better" than the corresponding class.

But the amount of memory used is not the whole story. Value types (structs) are fundamentally different than reference types (classes). Structs can be inconvenient to work with, and can actually cause performance problems if you're not careful.

The real answer, of course, is to use whichever works best in your situation. In most cases, you'll be much better off using classes.

like image 31
Jim Mischel Avatar answered Oct 05 '22 15:10

Jim Mischel