473,772 Members | 2,420 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6629
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.m icrosoft.comwro te in
message news:C7******** *************** ***********@mic rosoft.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
8699
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 return a pointer to an arbirary sized element in an array given an index and it will allocate the memory to back it if necessary: struct varray *tests = varray_new(sizeof(struct test)); struct test *t = (struct test *)varray_get(tests, 50));
3
2712
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 dynamic memory required for my datastructures ( used sizeof() to get the size of the structures ). But mallinfo() function show that approximately double that amount of memory is being allocated. I am working with gcc 3.2.2 on a redhat 9 machine. I...
11
3405
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 don't know how many points there will be in every struct in the end, so I have to allocate memory dynamically for them and can't use an array of fixed size, unfortunately. I would like to know if there is a better way to access struct members...
15
1927
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? (if it's relevent, use standard size of datatypes) a) Code: struct
11
3786
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
4771
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 that I found inside of a string. Any ideas?
24
19094
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 is faster than malloc, but dynamic memory allocation is more flexible. Please comment... thanks.
1
7976
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 compiled and run without any erros but the second program has a run time error when the function return from allocate and the ptr become NULL. How to fixed this? Second Program: /* Best Method to allocate memory for 2D Array because it's ...
2
2294
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 ago), I seem to remember that when using structures etc in C, you had to allocate memory to them then deallocate them when you had finished using them. My C# code creates a variable of type structure, passes it to the C++ DLL and then returns...
0
9619
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10103
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8934
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.