.NET Questions (CLOSED)

Questions and Answers on any aspect of .NET. Now closed.

This discussion group is now closed.

Have a question about .NET development? Try stackoverflow.com, a worldwide community of great developers asking and answering questions 24 hours a day.

The archives of .NET Questions contain years of Q&A. Even older .NET Questions are still online, too.

Stack and Heap


I was wondering where reference types store their value type members. For example, if I had the following:

class Circle{
  private double _radius;
  public Circle(double r){_radius = r;}

Circle c = new Circle(7.6);

Where is 7.6 stored? 

In the stack / heap drawings I'd have a reference "c" that pointed to an object on the heap of type Circle.  The radius member is a value type, but since it belongs to the class I am wondering if its also in the heap or if something on the heap points to it in the stack.

Thanks -
Digging Deeper Send private email
Wednesday, July 12, 2006
It is stored on the heap with the rest of the class.
johnny bravado
Wednesday, July 12, 2006
John is correct.

The memory allocated on the heap is of the size of the object being stored. The size of the object being stored is componsed (among other things) of all the data members of that object (primitives, value types, references to 'newed' data and otherwise).

It doesn't matter if you 'new' a data member, or put it in as a value, the type of variable (size of the reference, or the size of the data member itself) adds to the overall size of the object (Cirlce, in this case) being stored on the heap.
Andrey Butov Send private email
Wednesday, July 12, 2006
Found this on EE a while back when I was reading up:

- local variables (variables declared inside a function) are put on the stack - unless they are also declared as 'static' or 'register'
- function parameters are allocated on the stack
- local variables that are declared on the stack are not automatically initialized by the system so they usually have garbage in them until you set them
- variables on the stack disappear when the function exits (thus, if a function is called multiple times, it's local variables and parameters are recreated and destroyed each time the function is called end exited).

- declared variables (as opposed to dynamically created ie new, malloc) are created on the heap before program execution begins, they exist the entire life of the program (although scope may prevent access to them - they still exist) and they are initialized to all zeros
- global variables are on the heap
- static local variables are on the heap (this is how they keep their value between function calls)
- memory allocated by new, malloc and calloc are on the heap
redeye Send private email
Wednesday, July 19, 2006

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
Powered by FogBugz