473,320 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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.
Aug 1 '06 #1
5 2691
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

Aug 1 '06 #2
Johndoe wrote:
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
Aug 1 '06 #3
Johndoe <jo*****@johndoe.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 1 '06 #4
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 写é“:
Johndoe <jo*****@johndoe.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 1 '06 #5
"simida" <yo**********@gmail.comwrote:
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

--
http://barrkel.blogspot.com/
Aug 1 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: bitshadow | last post by:
using the following code, i was able to have my compiler seg fault on me when i gave the argument as anythng greater than 20,832,000bytes. In the case of the struct its 868 instances of said...
26
by: phoenix | last post by:
Hello, I've got a design question. I need to keep track of some variables and I am planning to put them inside a class or struct. Basically I'm talking about 10 bools, 20 ints and 2 arrays of...
5
by: Chua Wen Ching | last post by:
Hi there, I am very curious on this code: // declared as structure public struct Overlapped { public IntPtr intrnal; public IntPtr internalHigh;
11
by: dimension | last post by:
If my intentions are to create objects that encapsulates data rows in a table, is it better to use a Struct or Class? Based on what i read, if my objects will simply have get and set methods,...
9
by: thomson | last post by:
Hi all, Would you please explain me where will be the heap stored if it is declared inside the Class, As class is a reference type, so it gets stored on the heap, but struct is a value...
4
by: haitao.song | last post by:
Hi, As it is always stated that value type is allocated on stack, while reference types are on managed heap. How about the struct with string members? stuct A { string str; } String type is...
16
by: sarathy | last post by:
Hi all, I need a few clarifications regarding memory allocaion in C++. I apologize for the lengthy explanation. 1. In C++, Objects are allocated in heap. What does heap refer to? Is it an area...
37
by: JohnGoogle | last post by:
Hi, Newbie question... After a recent article in VSJ I had a go at implementing a Fraction class to aid my understanding of operator overloading. After a previous message someone suggested...
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.