[C#]An Array object is allocated in stack or heap?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mattmao
    New Member
    • Aug 2007
    • 121

    [C#]An Array object is allocated in stack or heap?

    Hello everyone, this is my first thread in this .NET forum.

    Since I am studying C#.NET in this semester, I reckon this would be just the right place for my asking questions regarding the C# language and the .NET framework:)

    I got some experience of ANSI C where you declare an array in stack, so:

    Code:
    int[10] myArray;
    would allocate a continous piece of memory in stack which can contain 10 integers in total.

    Now that we are in C#, where the Array is of type System.Array, it is an object rather than a "value type variable", isn't it?

    This gives us conveniences such like myArray.Sort(); but brings me the question of the above one:

    An Array object is allocated in stack or heap?
    So far as I know, "value type" variables such as the int and double are allocated in stack whereas "reference type" variables such as the Class object would be allocated in heap... This gives me quite some confusion as I couldn't tell when you:

    Code:
    int[] myArray = new int[10];
    this "myArray" is a reference to the Array object in heap or acts just as the same with ANSI C?




    Thanks in advance!
  • jjvainav
    New Member
    • Feb 2008
    • 25

    #2
    The array is allocated on the heap. But really, you can't think of the .NET heap the same way as in C or C++. Allocating a new object on the heap CAN be, but not always, faster then allocating memory on the stack in .NET.

    When a .NET application starts, a chunk of memory is allocated as the heap. So the memory is already allocated, when an object is created with the 'new' keyword, that object is given preallocated memory on the .NET heap.

    Originally posted by mattmao
    Hello everyone, this is my first thread in this .NET forum.

    Since I am studying C#.NET in this semester, I reckon this would be just the right place for my asking questions regarding the C# language and the .NET framework:)

    I got some experience of ANSI C where you declare an array in stack, so:

    Code:
    int[10] myArray;
    would allocate a continous piece of memory in stack which can contain 10 integers in total.

    Now that we are in C#, where the Array is of type System.Array, it is an object rather than a "value type variable", isn't it?

    This gives us conveniences such like myArray.Sort(); but brings me the question of the above one:

    An Array object is allocated in stack or heap?
    So far as I know, "value type" variables such as the int and double are allocated in stack whereas "reference type" variables such as the Class object would be allocated in heap... This gives me quite some confusion as I couldn't tell when you:

    Code:
    int[] myArray = new int[10];
    this "myArray" is a reference to the Array object in heap or acts just as the same with ANSI C?




    Thanks in advance!

    Comment

    Working...