Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is equivalent the memory used by an array of ints vs an array of structs having just one int?

Tags:

c#

.net

struct

Considering the next struct...

struct Cell
{
    int Value;
}

and the next matrix definitions

var MatrixOfInts = new int[1000,1000];
var MatrixOfCells = new Cell[1000,1000];

which one of the matrices will use less memory space? or are they equivalent (byte per byte)?

like image 425
Néstor Sánchez A. Avatar asked Nov 05 '14 04:11

Néstor Sánchez A.


People also ask

What is the correct way to define an array?

An array is a collection of elements of the same type placed in contiguous memory locations that can be individually referenced by using an index to a unique identifier. Five values of type int can be declared as an array without having to declare five different variables (each with its own identifier).

What is an array explain types of arrays?

An array type is a user-defined data type consisting of an ordered set of elements of a single data type. An ordinary array type has a defined upper bound on the number of elements and uses the ordinal position as the array index.

How to declare an int array in C++?

A typical declaration for an array in C++ is: type name [elements]; where type is a valid type (such as int , float ...), name is a valid identifier and the elements field (which is always enclosed in square brackets [] ), specifies the length of the array in terms of the number of elements.

Are structs contiguous in memory?

Because the contents of a struct are stored in contiguous memory, the sizeof operator must be used to get the number of bytes needed to store a particular type of struct, just as it can be used for primitives.


3 Answers

Both are the same size because structs are treated like any of the other value type and allocated in place in the heap.

long startMemorySize2 = GC.GetTotalMemory(true);
var MatrixOfCells = new Cell[1000, 1000];
long matrixOfCellSize = GC.GetTotalMemory(true);

long startMemorySize = GC.GetTotalMemory(true);
var MatrixOfInts = new int[1000, 1000];
long matrixOfIntSize = GC.GetTotalMemory(true);

Console.WriteLine("Int Matrix Size:{0}.  Cell Matrix Size:{1}", 
    matrixOfIntSize - startMemorySize, matrixOfCellSize - startMemorySize2);

Here's some fun reading from Jeffery Richter on how arrays are allocated http://msdn.microsoft.com/en-us/magazine/cc301755.aspx

like image 84
StackAlloc Avatar answered Oct 10 '22 00:10

StackAlloc


By using the sizeof operator in C# and executing the following code (under Mono 3.10.0) I get the following results:

    struct Cell
    {
        int Value;
    }

    public static void Main(string[] args)
    {
        unsafe
        {
            // result is: 4
            var intSize = sizeof(int);
            // result is: 4
            var structSize = sizeof(Cell);
        }
    }

So it looks like that an integer and a struct storing an integer consume the same amount of memory, I would therefore assume that arrays would also require an equal amount of memory.

like image 28
pgenfer Avatar answered Oct 09 '22 22:10

pgenfer


In an array with value-type elements, all of the elements are required to be of the exact same type. The object holding the array needs to store information about the type of elements contained therein, but that information is only stored once per array, rather than once per element.

Note that because arrays receive special handling in the .NET Framework (compared to other collection types) arrays of a structure type will allow elements of the structures contained therein to be acted upon "in-place". As a consequence, if one can limit oneself to storing a structure within an array (rather than some other collection type) and can minimize unnecessary copying of struct instances, it is possible to operate efficiently with structures of almost any size. If one needs to hold a collection of things, each of which will have associated with it four Int64 values and four Int32 values (a total of 48 bytes), using an array of eight-element exposed-field structures may be more efficient and semantically cleaner than representing each thing using four elements from an Int64[] and four elements from an Int32[], or using an array of references to unshared mutable class objects.

like image 35
supercat Avatar answered Oct 09 '22 22:10

supercat