473,776 Members | 1,650 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

121 New Member
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 10019
jjvainav
25 New Member
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
5461
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
10181
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 code... TCHAR myArray; DoStuff(myArray);
11
25401
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 array to 105 by 105 and it worked fine. Does anybody know what the problem is and how I could possibly get around it?
822
29785
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 real-time applications. The majority of expert/people recommend Ada for safety critical real-time applications. I've many years of experience in C/C++ (and Delphi) but no Ada knowledge. May I ask if it is too difficult to move from C/C++ to Ada?...
20
3504
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 "heap" or "stack" I'm curious to know the implemenations which dont have stack or heap.
13
17934
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 create another Area and assign myArea to it (ie: Area foo = myArea;) or is there a better way? ~AF
2
2197
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 instantiate an array of structures; consider the following useless code : using System; struct MyPointS
8
1511
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
3518
by: arunajob | last post by:
Hi all, If I have a piece of code something like this void main(void) { char * p1="abcdefghijklmn"; ............................................. }
0
9628
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10122
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10061
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6722
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5368
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5497
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4031
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3627
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2860
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.