Click here to Skip to main content
15,921,990 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When we are allocating memory in .net with array,is it dynamic or static??

i am thinking it is static ,but.. is there any possibility to manage by the CLR(making dynamic).. ??
Posted

Not static. The array is a reference object so it is allocated on heap (and is controlled by the Garbage Collector). With C++/CLI, which is very unusual compared with other high-level .NET languages, it's also possible to store it on stack or as a static memory. From this moment, let's forget about C++/CLI and manually-coded IL and get back to C#.

Memory allocation is not classified into static and "dynamic". There are three ways to store an object: in a memory area reserved for static data, on stack and on heap. Now, if you work with a reference object like an array, there are actually two separate objects: a reference and the referenced object itself which is always on heap.

If you consider a reference as an object, if can be stored anywhere: can be static, stack or heap object. It is static if it is a static field; it's on stack if this is a local variable. The other options comes when the reference is a non-static member of something. In this case, the storage of this reference member depends on how the instance of its declaring type is stored. Repeat these considerations recursively, and you will find out how things are stored in each particular case. Again, the referenced object itself does not depend on how reference is stored: it goes on heap.

The managed reference if a very special object. In most cased, you can consider it as a pointer, but this is not a pointer: a referenced object can be relocated, but the reference is maintained valid. If a reference is reachable from the running code in the application domain, the referenced object cannot be garbage-collected. Please see:
http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29[^].

—SA
 
Share this answer
 
Comments
samu4u 18-May-12 2:55am    
5
Sergey Alexandrovich Kryukov 18-May-12 10:25am    
Thank you.
Will you accept this answer formally (green button)? -- thanks.
--SA
VJ Reddy 18-May-12 8:35am    
Good answer. 5!
Sergey Alexandrovich Kryukov 18-May-12 10:23am    
Thank you, VJ.
--SA
Arrays in .Net are objects and allocated in the same way as pretty much everything else, so they're garbage collected, can be constructed at runtime, etc. I think this is 'dynamic' in the sense you mean it. However, you can't resize an array; maybe you're looking for List<T>?
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 17-May-12 13:48pm    
Basically correct, but perhaps not that trivial. Everyone needs to understand how reference object itself, as opposed to the referenced object, is stored.
I tried to explain all that, please see my answer.
--SA
samu4u 18-May-12 2:56am    
5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900