473,396 Members | 1,871 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,396 software developers and data experts.

Where are ref types that are members of value types stored?

Structs are value types, strings are ref types. If you have something
like this:

struct foo
{

System.String str;
}

foo TheDayToday;

then, where's

TheDayToday.str

going to be stored?

(a) The string is going to be stored on the stack;
(b) The struct is going to be stored on the stack with a 32-bit
System.IntPtr/System.UInt32 also copied on the stack holding the
address/reference to the actual string allocated on the heap.

Nov 21 '05 #1
4 1383
Can I take 50/50 ? :)

b.


"Sathyaish" <Sa*******@Yahoo.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
Structs are value types, strings are ref types. If you have something
like this:

struct foo
{

System.String str;
}

foo TheDayToday;

then, where's

TheDayToday.str

going to be stored?

(a) The string is going to be stored on the stack;
(b) The struct is going to be stored on the stack with a 32-bit
System.IntPtr/System.UInt32 also copied on the stack holding the
address/reference to the actual string allocated on the heap.

Nov 21 '05 #2
Sathyaish,
This appears to be a C# question, being asked in a VB.NET newsgroup, sample
auto-translated into VB.NET.

Public Structure foo
Public str As String
End Structure

Public Sub Main()
Dim TheDayToday As foo
End Sub

The TheDayToday variable is on the stack, "foo" being a value type its
content is also on the stack. Which means the str variable, the variable
itself, is on the stack. However String being a reference type means the
actual value of the string is on the heap. In other words str on the stack
holds a reference to a String Object on the Heap.

Hope this helps
Jay

"Sathyaish" <Sa*******@Yahoo.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
| Structs are value types, strings are ref types. If you have something
| like this:
|
| struct foo
| {
|
| System.String str;
| }
|
| foo TheDayToday;
|
| then, where's
|
| TheDayToday.str
|
| going to be stored?
|
| (a) The string is going to be stored on the stack;
| (b) The struct is going to be stored on the stack with a 32-bit
| System.IntPtr/System.UInt32 also copied on the stack holding the
| address/reference to the actual string allocated on the heap.
|
Nov 21 '05 #3
Sathyaish,

I saw a same question answered in this newsgroup

http://groups-beta.google.com/group/...98912b823353dd

I hope this helps,

Cor
Nov 21 '05 #4
Thanks, Jay and Cor.

Nov 21 '05 #5

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

Similar topics

2
by: Sathyaish | last post by:
Structs are value types, strings are ref types. If you have something like this: struct foo { System.String str; } foo TheDayToday;
8
by: VJ | last post by:
Hi, I apologize if this question has been asked many times on the group! I am new to programming, I know that there are three section in address space- one for code, one for stack, and the...
23
by: rohit | last post by:
Hi, In my couple of years of experience, I have never found a single instance where I needed to use unions and bitfields(though I have used structures).I was just imagining where would these find...
42
by: x-pander | last post by:
Is is guaranteed, that a pointer to any object in C points exactly at the lowest addressed byte of this object? Specifcally is it possible for any platform/os/compiler combination that: (char...
5
by: Zach | last post by:
When it is being said that, "value types are created on the stack or inline as part of an object". If a value type is created in an object, and that object is being called, the value type in that...
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: Ben R. | last post by:
I'm curious about the differeng behavior of the "new" keyword when dealing with value versus object types. If I'm correct, when I do: dim x as integer There's no need for "new" to be called...
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...
46
by: clintonG | last post by:
Documentation tells me how but not when, why or where... <%= Clinton Gallagher http://msdn2.microsoft.com/en-us/library/saxz13w4(VS.80).aspx
12
by: Sam Kong | last post by:
Hi, JavaScript hides its memory structure. I know that numbers, booleans, null and undefined are value types (value is directed saved in a variable). I want to know: - How JavaScript...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
jinu1996
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...
0
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...
0
tracyyun
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...
0
agi2029
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 project—planning, coding, testing,...

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.