Call by ref ... stack or heap?

R

Raj

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

Raj
 
C

Christoph Basedau

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
 
P

Peter Duniho

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top