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

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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";
.



Relevant Pages

  • Re: A solution for the allocation failures problem
    ... struct nested *ptr1; ... which would jump to label if the allocation failed. ... implemented for the stack under windows. ... So in the malloc instance I would say you make use of the pre-allocated reserve rather than freeing it so you can do further mallocs whilst recovering. ...
    (comp.lang.c)
  • Re: Would a string instance become a value type if it was a member of a struct?
    ... In .net, a struct is a value type, so it gets stored on the stack. ... a string is a reference type. ... Actually any object's data also contains two references for internal use, and the String object probably also contains a length variable. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Stack
    ... members of the stack at any given time. ... struct stackelement{ ... struct stackelement item; ... The string you were using was an automatic variable. ...
    (comp.lang.c)
  • Re: Would a string instance become a value type if it was a member of a struct?
    ... In .net, a struct is a value type, so it gets stored on the stack. ... struct foo ... a string is a reference type. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Structs
    ... >> A struct is created, generally, in null memory. ... >> memory model ensures that a variable created on the stack retains a zero ... Note "Local variables are initialized to 0 before entry if the initalize ... case the C# compiler generates superflous code with the initobj instruction. ...
    (microsoft.public.dotnet.languages.csharp)