473,398 Members | 2,812 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,398 software developers and data experts.

A simple question on heap objects.

78
Hi all. I'd like to know something. This may be silly but, if there is an array inside a heap object, where will the array reside?
If in heap too, then how does its mem location get freed?

I can't get my mind worked around it.
Thanks!
May 9 '07 #1
10 1636
JosAH
11,448 Expert 8TB
Hi all. I'd like to know something. This may be silly but, if there is an array inside a heap object, where will the array reside?
If in heap too, then how does its mem location get freed?

I can't get my mind worked around it.
Thanks!
If an array is a member of a structure/class object it is just part of the memory
allocated for the entire structure/class object. If the entire thing was stored on
the stack (think of local variables), the array will be part of it and also stored on
the stack. If the thing resides somewhere in the heap (think of malloc or new)
the array will be somewhere in there too.

Here's an extremely simple example in C; consider the following struct:
Expand|Select|Wrap|Line Numbers
  1. typedef struct {
  2.    int a;
  3.    char b[4];
  4.    int c;
  5. } simple_t;
  6. ...
  7. simple_t p*= malloc(sizeof(simple_t));
Suppose malloc returned memory at location 0x1000. Then the struct is stored
at location 0x1000 and p contains that value. The structure layout is:

0x1000: a
0x1004: 1st elem of b
0x1005: 2nd elem of b
0x1006: 3rd elem of b
0x1007: 4th elem of b
0x1008: c
0x100C: other memory

kind regards,

Jos

ps. I ignored internal/trailing padding for simplicity.
May 9 '07 #2
Sebouh
78
Yes, now i remember. But how does it get freed?
May 9 '07 #3
JosAH
11,448 Expert 8TB
Yes, now i remember. But how does it get freed?
given the example from my previous reply:
Expand|Select|Wrap|Line Numbers
  1. free(p);
kind regards,

Jos
May 9 '07 #4
AdrianH
1,251 Expert 1GB
What Jos is saying is that the array is located in the object, you delete the object, you delete the array.


Adrian
May 9 '07 #5
Sebouh
78
given the example from my previous reply:
Expand|Select|Wrap|Line Numbers
  1. free(p);
kind regards,

Jos
Not p, the array.
So what Adrian said.
What about:
Expand|Select|Wrap|Line Numbers
  1. struct p {
  2.     int *i = (int*)malloc(8);
  3. }
  4.  
Does i get freed if i free p (p being a pointer to a struct p) or that works only for arrays?
I'm asking cause i is defined and declared at the same time like with an array.
May 9 '07 #6
AdrianH
1,251 Expert 1GB
What about:
Expand|Select|Wrap|Line Numbers
  1. struct p {
  2.     int *i = (int*)malloc(8);
  3. }
  4.  
Does i get freed if i free p (p being a pointer to a struct p) or that works only for arrays?
I'm asking cause i is defined and declared at the same time like with an array.
The code you posted is not valid, so I cannot answer your question. Perhaps restate your question?


Adrian
May 9 '07 #7
Sebouh
78
The code you posted is not valid, so I cannot answer your question. Perhaps restate your question?


Adrian
Sorry, you're right that's invalid.
But if i malloc in constructor, must i create a destructor to free it?
May 9 '07 #8
weaknessforcats
9,208 Expert Mod 8TB
You should not be using malloc/free in constructors. Instead, use the new/delete of C++. malloc/free are functions that do not call constructors ordestructors on the members they are allocate or delete. Use malloc/free only in C.

The short answer to your question is: If you allocated, then you must delete. A destructor is a convenient place to delete because you don't have to remember to call it when your object is destroyed.
May 9 '07 #9
AdrianH
1,251 Expert 1GB
You should not be using malloc/free in constructors. Instead, use the new/delete of C++. malloc/free are functions that do not call constructors ordestructors on the members they are allocate or delete. Use malloc/free only in C.

The short answer to your question is: If you allocated, then you must delete. A destructor is a convenient place to delete because you don't have to remember to call it when your object is destroyed.
What weakness said. :)


Adrian
May 10 '07 #10
Sebouh
78
OK, thank you guys.:)
May 10 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: ANt | last post by:
Hi, we have some major GC issues at present with a system we're trying to put live. It's a live calculation engine that's distributed across about 30 Java server processes. A set of processes...
14
by: Kevin Grigorenko | last post by:
Hello, I couldn't find an obvious answer to this in the FAQ. My basic question, is: Is there any difference in allocating on the heap versus the stack? If heap or stack implementation is not...
4
by: Shailesh Humbad | last post by:
If a class has no constructor/destructor, is not part of an inheritance heirarchy, is not a template, and only contains members of integral types, then is it okay to allocate it off the heap? Is...
17
by: Jonas Rundberg | last post by:
Hi I just started with c++ and I'm a little bit confused where stuff go... Assume we have a class: class test { private: int arr; };
14
by: Olumide | last post by:
In the following C++ class, MyClass{ // Members } From which I create two objects (using the default constructor), (1) MyClass *object1 = new MyClass(); // Stack (2) MyClass object2 =...
13
by: Jason Swett | last post by:
I want to do graphics with C++. Surprisingly, so far nobody has been able to tell me anything helpful. How do I do it? Any input would be greatly appreciated. Jason
10
by: Shuo Xiang | last post by:
Greetings: I know that variables declared on a stack definitely does not reside in heap space so there is only a very limited amount of stuff that you can store in the stack of a function. But...
16
by: sarathy | last post by:
Hi all, I need a few clarifications regarding memory allocaion in C++. I apologize for the lengthy explanation. 1. In C++, Objects are allocated in heap. What does heap refer to? Is it an area...
53
by: fdmfdmfdm | last post by:
This is an interview question and I gave out my answer here, could you please check for me? Q. What are the memory allocation for static variable in a function, an automatic variable and global...
9
by: coder_lol | last post by:
Thanks everybody for helping me with the Syntax confusion! The implicit conversion stuff really got me :) I have one more question... Array<int32ia; Does the above use the default...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.