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

struct with string, where does it allocate to? heap?

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
pointer (integer value) to a non-mutable reference string on heap.

Some suggestions? How does it work? Under GC, does all copies
of A gets updated with the string reference value?

Mar 27 '06 #1
4 6169
using System;

using System.Collections.Generic;

using System.Text;

namespace construct

{

class Program

{

public struct A

{

private string str;

public string Str

{

set { this.str = value; }

get { return this.str; }

}

}

public class B

{

private string str;

public string Str

{

set { this.str = value; }

get { return this.str; }

}

}

public static void fooA(A a)

{

a.Str = "Assigned FooA";

}

public static void fooB(B b)

{

b.Str = "2";

}

static void Main(string[] args)

{

A ast = new A();

B bst = new B();

ast.Str = "1";

fooA(ast);

fooB(bst);

System.Console.WriteLine(ast.Str);

System.Console.WriteLine(bst.Str);

System.Console.ReadLine();

}

}

}
---- Looks like it does allocate on stack

Mar 27 '06 #2
> As it is always stated that value type is allocated on stack, while reference types are on managed heap.

A lot of books state this. It is an oversimplification. The truth is:

1. Reference types are allocated on the managed heap.
2. Value types that are local variables or method parameters are
allocated on the stack. This includes references to reference types
(which are, themselves, just values).
3. Value types that are members of a reference type (for example the
"int"s that form part of an object's state) are allocated along with
and in-line with all of the other data for the reference type, on the
managed heap.

Your assumption about a value type that contains a reference to a
string is correct: the values that make up the value type will be
allocated wherever that value type is allocated. If it is a local
variable or a method parameter, on the stack. If it is a member in a
class definition, then on the heap with the rest of the data for the
class.

The value type will contain as part of its value a reference to the
string, which will be allocated on the managed heap.

Remember that strings are immutable, so one cannot make a change to the
string and have all value types that reference it change together,
although your assumption that all value types point to the same string
on the heap would be true (depending upon how the string was
constructed and how its reference is placed in the struct).

Your theory works out better if you choose something other than a
string. Take, for example, a value type that is a Measure:

public struct Measure
{
private decimal _quantity;
private UnitOfMeasure _units;

public Measure(decimal quantity, UnitOfMeasure units)
{
...
}
}

Assuming that UnitOfMeasure is a reference type, then the struct will
contain: a decimal value, and a reference. If multiple Measures are
constructed pointing to the same UnitOfMeasure, then they all contain
the same reference to the same unit of measure object on the managed
heap.

If the UnitOfMeasure class were mutable (a bad idea, in this case, but
say it were) then a change to a unit of measure object would have an
effect in all Measure structs that point to it, just as you surmised.
That's why I made my UnitOfMeasure class immutable. :)

Mar 27 '06 #3
Really appreciate... thx!!

Mar 27 '06 #4
using System;

using System.Collections.Generic;

using System.Text;

namespace construct

{

class Program

{

public class IntClass

{

private int x;

public IntClass() { x = 0; }

public int Val { get { return x; } set { x = value; } }

}

public struct A

{

private IntClass xint;

public void setInt() { xint = new IntClass(); }

public IntClass XInt

{

set { this.xint = value; }

get { return this.xint; }

}

public int X

{

set { xint.Val=value; }

get { return xint.Val; }

}

}

public static void fooA(A a)

{

a.XInt = new IntClass();

}

static void Main(string[] args)

{

A a = new A();

a.setInt();

a.X = 6;

fooA(a); // should still be 6;

System.Console.WriteLine(a.X); //6

A b = a;

b.X = 5;

System.Console.WriteLine(a.X); // both to 5
System.Console.WriteLine(b.X); // both to 5

b.XInt = new IntClass(); // the reference wont get updated into all
copies of the value types.
System.Console.WriteLine(a.X); // 5
System.Console.WriteLine(b.X); // 0

System.Console.ReadLine();

}

}

}

Mar 27 '06 #5

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

Similar topics

0
by: Uday Deo | last post by:
Hi everyone, I have a .NET app invoking from the ASP page. It has to load bunch of data during first visit. The app works fine on the Staging and Prod server. This is the first time the company...
5
by: Alfonso Morra | last post by:
Hi, Doest the c_str() method of the string class allocate memory. if it does alloc mem behind the scenes - whose responsibility is it to cleanup after (i.e. free the returned char*?) Tkx
0
by: AshleyT | last post by:
I am trying to get the user authenticated name and then get the last 3 digits of that string. Below is the first part of my code. Do I need to do this in a class, or namespace? It seems that I...
0
by: Gaurav | last post by:
Hi, I have a connection string "Data Source=db1.mynet;DATABASE=test;User ID=testuser;Password=test;" , when i use the connection string to get a dataset of all the tables of a database it...
1
by: openleren | last post by:
Hi all, with a little help from my friends, I am trying to construct a connectionstring from a relative path in web.config. It contains <add key="conAccess" value="microsoft.jet.oledb.4.0;data...
0
by: andy | last post by:
Hi all, Any help much appreciated - Andy I am trying to connect to a SQL Server DB using c#, ADO.Net mySqlConnection = new...
1
by: sal | last post by:
Greets, All Question with udl and connection string does it work? I'm having problems with the code below I keep getting an error "Keyword not supported 'File Name'" I was just following...
2
by: lovecreatesbea... | last post by:
If the built-in operator keyword new doesn't allocate memory on heap and it calls global operator new (::operator new) or class member operator new to do that. What are the two kinds of operator...
19
by: SPABBOJU | last post by:
Hi guys... I want to check whether string does not exist with out using !~ . basical for character we check like that using . How to do the same for word, sentence ?.... I...
1
by: lye85 | last post by:
#include <stdio.h> #include <stdlib.h> #include <string.h> struct account { char AccName; int Age; double AccBalance; struct account *Next;
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.