struct on the stack... sometimes on the heap?

  • Thread starter Thread starter Johndoe
  • Start date Start date
J

Johndoe

Hi,

A struct lives on the stack, right? What happens if I create a struct that
contains a reference type?

I'm guessing that there will be a pointer on the stack refering to my
reference type. I'm not 100% sure. Can someone please confirm?

Thanks.
 
As I understand it...
A struct lives on the stack, right?

As a variable, yes; as a field in a class, no - it goes on the heap as
part of the object
What happens if I create a struct that
contains a reference type?

A struct (or indeed a class) never "contains" (in terms of memory
layout) a reference type; it contains (as you say later) a reference
(essentially an integeric field) to the reference tpe (which always
lives on the heap in managed code). Contrast that the other way around;
a class (or indeed a struct) with a struct field actually has a block
of memory (in it's own definition) reserved for the contained struct
(this doesn't hold true for arrays, which are referenced).
I'm guessing that there will be a pointer on the stack refering to my
reference type.

(key word: stack) - or heap, depending. But essentially you have it
correct.

Marc
 
Johndoe said:
Hi,

A struct lives on the stack, right? What happens if I create a struct
that contains a reference type?

I'm guessing that there will be a pointer on the stack refering to my
reference type. I'm not 100% sure. Can someone please confirm?

Exactly.

If you create a reference type that contains a struct, then you'll get a
struct on the heap. Anything that results in boxing (e.g. converting to
object, calling a virtual function, etc) will also result in a struct on the
heap.

-cd
 
Johndoe said:
A struct lives on the stack, right?

Not always. A struct which is part of a reference type will be on the
heap. The data for the struct lives wherever the variable lives,
basically. See http://www.pobox.com/~skeet/csharp/memory.html
What happens if I create a struct that
contains a reference type?

I'm guessing that there will be a pointer on the stack refering to my
reference type. I'm not 100% sure. Can someone please confirm?

That's correct. Note that the referenced *object* isn't really part of
the struct - only the reference is.
 
Yes, a reference that stored in the struct just like 32BIT INT value.
the reference value maybe contain a referenced *object* 32 BIT address
that located in the heap. Of course, it is a simple explanation.

(Maybe Like Handler?)


Jon 写é“:
 
simida said:
Yes, a reference that stored in the struct just like 32BIT INT value.
the reference value maybe contain a referenced *object* 32 BIT address
that located in the heap. Of course, it is a simple explanation.

A reference value stored anywhere is a pointer (of size IntPtr.Size -
changes between 32 / 64 etc. versions of the runtime) to an object on
the heap. The reference value is only and exactly that, a pointer (i.e.
an address) to the heap, nothing more.

-- Barry
 
Back
Top