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. 5 2720
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 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
Johndoe <jo*****@johndo e.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.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
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*****@johndo e.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.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
"simida" <yo**********@g mail.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/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 structure. The
compiler obviously allows VLA however it craps out after the above
amount of bytes.
I was told i was attempting to put everythng on the stack and not the
heap. So i was wondering if anyone can maybe clear it up, is that true?
would i...
|
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 ints. The size of the arrays would depend
on some external value (going from 0 to around 1000 max). I would have an
array of max 255 of these classes/structs (in most cases it will be less
then 5 however)
Since there's no real business logic my...
|
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;
|
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, Struct is may be better...but i
am looking for some advise from the experts on this?
Assumptions: it is possible for some operations, i may have 8000 to 10000
(or more) objects instantiated.
what are some considerations in designing a system that...
|
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 type-stored on the stack
Regards
thomson
| |
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 considered as reference type.... My guess is the
struct A is actually a valuetype with a string reference/address.
So it acts as if struct A { int ptrStr; } Anyway, the ptrStr is like a
|
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 in RAM/Memory or does it refer
to a data structure being used for storing objects.
2. In C++, functions and its local variables go in stack.
If local variables that are primitives go in stack, it is OK. But what
|
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 that I implement it as a struct rather than a
class which I did and all worked OK.
The simplest declaration for the struct is:
|
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 creation of this
implicit default constructor, to force the creation of a struct via my
constructor only?
Zytan
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |