Call by ref ... stack or heap?

  • Thread starter Thread starter Raj
  • Start date Start date
R

Raj

While invoking a function using ref, where the actual and formal parameters
loaded in stack or heap?

Raj
 
Raj said:
While invoking a function using ref, where the actual and formal parameters
loaded in stack or heap?

Depends.
A ref parameter uses the slot of memory of the caller,
so it's the allocation-type of the variable in
calling context.
Reference-type variables are heap-allocated.
Value-type variables are heap-allocated only if they're instance
variables aka "fields".
Otherwise - as local variables incl. params - value type variables
are stack-allocated.
Static variables are always heap-allocated regardless of their
type.

see:
http://www.yoda.arachsys.com/csharp/memory.html

Christoph
 
Raj said:
While invoking a function using ref, where the actual and formal parameters
loaded in stack or heap?

See Christoph's reply, as well as Jon's article for description of
exceptions.

That said, you should not care at all whether the data is stored on the
stack or heap. That's an implementation detail and, while unlikely to
change, is not at all required to stay the same for future versions of
C#/.NET.

One of the points of writing "managed code" is to distance yourself from
these kinds of implementation details. If you are concerned about them,
there's a good chance they are interfering with good design of your code.

Pete
 
Back
Top