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

Memory allocation of structs?

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
4 6597
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Michael B. Allen | last post by:
Coming from C and Java on *nix I'm a little out of my element messing around with CList and MSVC++ but I think my issues are largely syntactic. I have an ADT that I use called a 'varray' that can...
3
by: Sourin | last post by:
Hi all, I am trying to write code for my experiments. My work involves huge datasets, and as such my code needs to be memory efficient. I did some hand calculations regarding the amount of...
11
by: Roman Hartmann | last post by:
hello, I do have a question regarding structs. I have a struct (profil) which has a pointer to another struct (point). The struct profil stores the coordinates of points. The problem is that I...
15
by: puzzlecracker | last post by:
Got Confused on the interview with memory alligment questions... PLEASE HELP -- How much bytes of memory will structs below take on 32 bit machine? What about 64 bit machine? Why is it different?...
11
by: skumar434 | last post by:
Hi everybody, I am faceing problem while assigning the memory dynamically to a array of structures . Suppose I have a structure typedef struct hom_id{ int32_t nod_de; int32_t hom_id;
94
by: smnoff | last post by:
I have searched the internet for malloc and dynamic malloc; however, I still don't know or readily see what is general way to allocate memory to char * variable that I want to assign the substring...
24
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
2
by: Andy Baker | last post by:
I have recently written a .NET wrapper for a C++ DLL file and it seems to be working well, but I do have some concerns about memory. From my limited C programming experience (college 15+ years...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.