Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is array allocated on stack in c#?

I've been trying to figure out when things get allocated on stack and I can't figure out how would you make array (or rather values in it) get allocated on stack;

in this example:

public void foo() {     int bar[] = new int [10]; } 

10 int structs would be allocated on the the heap, only pointer to them would be on stack, correct?

How would one make fixed size array to go on stack? What if I'm using stucts I defined?

What if I want array size passed as parameter to function?

As far as I understand there should be no problem to get array of arbitrary size on stack when function is called, if size is known when function is called.

Should I even be bothered by this? As far as I understand getting this fixed size array on stack would improve performance, because no heap allocation is done.

like image 762
morowinder Avatar asked Aug 28 '15 15:08

morowinder


People also ask

Are arrays allocated on stack in C?

Unlike Java, C++ arrays can be allocated on the stack.

Where is an array allocated in C?

Array bucket values are stored in contiguous memory locations (thus pointer arithmetic can be used to iterate over the bucket values), and 2D arrays are allocated in row-major order (i.e. the memory layout is all the values in row 0 first, followed by the values in row1, followed by values in row 2 ...).

Is array stored in stack or heap?

Storage of Arrays As discussed, the reference types in Java are stored in heap area. Since arrays are reference types (we can create them using the new keyword) these are also stored in heap area.


1 Answers

10 int structs would be allocated on the the heap, only pointer to them would be on stack, correct?

Yes, correct.

How would one make fixed size array to go on stack? What if I'm using stucts I defined?

The stackalloc keyword serves this purpose. However, this works in unsafe context only, which is a rather unnecessarily limiting factor in most scenarios, not worth the performance tradeoff.

Example:

public void unsafe foo() {     int* bar = stackalloc int [10]; } 

You will have to use pointer arithmetic to access members of the array.

What if I want array size passed as parameter to function? As far as I understand there should be no problem to get array of arbitrary size on stack when function is called, if size is known when function is called.

Works as expected:

public void unsafe foo(int length) {     int* bar = stackalloc int [length]; } 

Should I even be bothered by this? As far as I understand getting this fixed size array on stack would improve performance, because no heap allocation is done.

No, in general not. Unless you deal with some very specific performance-critical scenarios, like heavy math computations, encryption, compression etc., it brings no real benefit.

Also, see this question for performance-related discussion.

like image 105
Ondrej Tucny Avatar answered Oct 03 '22 16:10

Ondrej Tucny