Would a string instance become a value type if it was a member of astruct?

S

Sathyaish

And I've probably asked this, like, a thousand times.

In .net, a struct is a value type, so it gets stored on the stack.

struct foo
{
int a;
byte b;
}

In the above code, both variables, a and b, would get stored on the
stack. Therefore, the sizeof(foo) would be 5 bytes (or 8 bytes
depending on compiler switches that govern padding and byte alignment.
In .net, this would be the StructLayoutKindAttribute)

But that's beside the point. Let me get to my question.

In .net, a string is a reference type. So:

string bar = "CoT"; //Unicode encoding by default

would mean bar (4 bytes) on the stack that reference 8 bytes (2*
(3+'\0')) on the heap.


My question:

Where would a string that is a member of a struct be stored?

struct foo
{
string bar;
}

foo f;
f.bar = "CoT";
 
A

Anthony Jones

Sathyaish said:
And I've probably asked this, like, a thousand times.

In .net, a struct is a value type, so it gets stored on the stack.

struct foo
{
int a;
byte b;
}

In the above code, both variables, a and b, would get stored on the
stack. Therefore, the sizeof(foo) would be 5 bytes (or 8 bytes
depending on compiler switches that govern padding and byte alignment.
In .net, this would be the StructLayoutKindAttribute)

But that's beside the point. Let me get to my question.

In .net, a string is a reference type. So:

string bar = "CoT"; //Unicode encoding by default

would mean bar (4 bytes) on the stack that reference 8 bytes (2*
(3+'\0')) on the heap.


My question:

Where would a string that is a member of a struct be stored?

struct foo
{
string bar;
}

foo f;
f.bar = "CoT";

A string is a reference type. The struct above would remain the same very
small size regardless of how big the string is.

The string is allocated on the heap and then all things assigned from it
would just point at that instance of the string.

foo f;
f.bar = "CoT";

foo g;
g.bar = f.bar

There is only one instance of a string containing "CoT" allocated somewhere
in memory. Both f and g will contain references to this same instance.
 
S

Sathyaish

A string is a reference type.  The struct above would remain the same very
small size regardless of how big the string is.

The string is allocated on the heap and then all things assigned from it
would just point at that instance of the string.

foo f;
f.bar = "CoT";

foo g;
g.bar = f.bar

There is only one instance of a string containing "CoT" allocated somewhere
in memory.  Both f and g will contain references to this same instance.

Thank you, Anthony.
 
G

Göran Andersson

Sathyaish said:
And I've probably asked this, like, a thousand times.

In .net, a struct is a value type, so it gets stored on the stack.

Well, that is not always true.

As a local variable the struct is stored on the stack.

As a member of an object the struct is stored as part of the object's
data on the heap.

As part of another struct it will be stored as part of that struct's
data, wherever that is stored.
struct foo
{
int a;
byte b;
}

In the above code, both variables, a and b, would get stored on the
stack. Therefore, the sizeof(foo) would be 5 bytes (or 8 bytes
depending on compiler switches that govern padding and byte alignment.
In .net, this would be the StructLayoutKindAttribute)

But that's beside the point. Let me get to my question.

In .net, a string is a reference type. So:

string bar = "CoT"; //Unicode encoding by default

would mean bar (4 bytes) on the stack that reference 8 bytes (2*
(3+'\0')) on the heap.

Actually any object's data also contains two references for internal
use, and the String object probably also contains a length variable (so
that you don't have to loop through the entire string each time you need
to get the length).
My question:

Where would a string that is a member of a struct be stored?

struct foo
{
string bar;
}

foo f;
f.bar = "CoT";

The reference will be stored as part of the struct's data, wherever that
is stored.

The string literal already exists as a constant in the assembly, so the
reference will just point to that data.

If you would create a new string instance, e.g.:

f.bar = new String(new char[]{'C','o','T'});

then the string data would be stored on the heap.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top