Memory allocation of structs?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I read structs are stored at stack area or inline-heap based objects.

What is meant by inline-heap based objects? I didnt get that.

Thanks,
Veera.
 
Structs (and all value variables) are stored relative to the base of their
enclosing scope allocation. Basically this means that when used in a local
variable, they are stored on that particular thread's stack and the compiler
generates offsets to the current stack frame pointer. When a struct is part
of an object variable (class), it is stored as part of that object's data
area and referenced relative to the base of the object's memory allocation.

Structs that are part of objects are stored in the heap, but they are not
directly garbage collected by the runtime. When the object they are part is
garbage collected, the struct effectively disappears from unreachable memory
as well.

Mike Ober.
 
veera said:
I read structs are stored at stack area or inline-heap based objects.

What is meant by inline-heap based objects? I didnt get that.

structs are allocated in exactly the way that ints or doubles are
allocated.

So, if you declare a class:

public class MyClass
{
private int _field1;
private double _field2;
private MyStructType _field3;
...
}

then when an instance of MyClass is created on the heap, the storage
reserved for that instance on the heap will contain space for an int, a
double, and a MyStructType.

This is different from the way that class instances are stored. If you
change your MyClass definition to

public class MyOtherClass
{
private int _field1;
private double _field2;
private MyThirdClass _field3;
...
}

then when an instance of MyOtherClass is created on the heap, the
storage reserved for that instance on the heap will contain space for
an int, a double, and a _reference_, which may be null or may contain a
reference to a MyThirdClass instance, which would be stored elsewhere
on the heap.

Notice that space for "struct" instances are stored _inline_, that is
alongside ints and doubles, whereas space for "class" instances are
allocated their own storage and references to them are stored _inline_.

Does that make more sense?
 
veera said:
I read structs are stored at stack area or inline-heap based objects.

What is meant by inline-heap based objects? I didnt get that.

I would guess, this wording describes a situation when some
class has a member of struct type. Then, when an instance of
the class is created (on heap), that member gets memory allocated
for it "inline", i.e. inside the same memory block as other
value type members of the class. In other words, the whole
object of struct type will be sitting inside the memory allocated
for the class instance.
 
Good One.... Thank You.

Bruce Wood said:
structs are allocated in exactly the way that ints or doubles are
allocated.

So, if you declare a class:

public class MyClass
{
private int _field1;
private double _field2;
private MyStructType _field3;
...
}

then when an instance of MyClass is created on the heap, the storage
reserved for that instance on the heap will contain space for an int, a
double, and a MyStructType.

This is different from the way that class instances are stored. If you
change your MyClass definition to

public class MyOtherClass
{
private int _field1;
private double _field2;
private MyThirdClass _field3;
...
}

then when an instance of MyOtherClass is created on the heap, the
storage reserved for that instance on the heap will contain space for
an int, a double, and a _reference_, which may be null or may contain a
reference to a MyThirdClass instance, which would be stored elsewhere
on the heap.

Notice that space for "struct" instances are stored _inline_, that is
alongside ints and doubles, whereas space for "class" instances are
allocated their own storage and references to them are stored _inline_.

Does that make more sense?
 

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

Back
Top