By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,694 Members | 1,294 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,694 IT Pros & Developers. It's quick & easy.

Memory allocation of structs?

P: n/a
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.
Jul 21 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
veera sekhar kota wrote:
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?

Jul 21 '06 #2

P: n/a
veera sekhar kota wrote:
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.
Jul 21 '06 #3

P: n/a
Good One.... Thank You.

"Bruce Wood" wrote:
veera sekhar kota wrote:
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?

Jul 21 '06 #4

P: n/a
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 sekhar kota" <ve*************@discussions.microsoft.comwrote in
message news:C7**********************************@microsof t.com...
>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.


Jul 21 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.