473,386 Members | 2,042 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

121 100+
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:

Expand|Select|Wrap|Line Numbers
  1. 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:

Expand|Select|Wrap|Line Numbers
  1. 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!
Mar 6 '08 #1
1 9844
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.

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:

Expand|Select|Wrap|Line Numbers
  1. 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:

Expand|Select|Wrap|Line Numbers
  1. 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!
Mar 6 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Arthur Sinko | last post by:
Hi, Is it possible to answer the following question: what is the difference between two declarations: vector<double> a(n); and double a;
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
11
by: eggie2486 | last post by:
What is the maximum size of an array? I tried to edit an extremely large array for a magic square, for example, array, and when I ran the program, it would not display the array. I changed the...
822
by: Turamnvia Suouriviaskimatta | last post by:
I 'm following various posting in "comp.lang.ada, comp.lang.c++ , comp.realtime, comp.software-eng" groups regarding selection of a programming language of C, C++ or Ada for safety critical...
20
by: Sushil | last post by:
Hi gurus I was reading FAQ "alloca cannot be written portably, and is difficult to implement on machines without a conventional stack." I understand that the standard does not mandate...
13
by: Abe Frohnman | last post by:
Hello all, I'm passing a reference to a class into the constructor of a form, like so: public MyForm(int count, ref Area myArea) {...} How can I use myArea outside the constructor? Should I...
2
by: david | last post by:
Well, as a matter of fact I_HAD_MISSED a basic thing or two, anyway, although Ollie's answer makes perfectly sense when dealing with classes, it doesn't seem to me to apply as well if you have to...
8
by: junky_fellow | last post by:
Guys, Consider the following snippet of code: int main(VOID) { static ushort fractionalValue={ 0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 250, 333, 666, 750, 0, 0 };
50
by: arunajob | last post by:
Hi all, If I have a piece of code something like this void main(void) { char * p1="abcdefghijklmn"; ............................................. }
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.