Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hello everyone, I have questions

Status
Not open for further replies.

jianhuay

Programmer
Apr 11, 2001
13
0
0
AU
Hello everyone,

I have questions concerned with 'dynamic memory allocation' using C++ operators new and delete. I did search questions posted in this forum about 'dynamic memory allocation', but I found few questions talking about this and no answer to my questions. So I think I may need to post my questions here for clear solutions from experts.

The first scenery of usage of 'dynamic memory allocation': I have two classes, ClassA and ClassB. The former class calls functions of the latter to setup its member data. The following code shows basic skeleton of ClassA and ClassB.

class ClassA {
double ** data;
int * index;
//...

ClassA ( ) {
data = NULL;
index = NULL;

init ( );
}

void init ( ) {
ClassB objB;
objB.loadData(&data, &index /*...*/);
}
//...
};

class ClassB {
ClassB ( ) {/*...*/}

void loadData ( *** data_ , ** index_ /*...*/) {
int m, n;
double ** data = NULL;
int *index = NULL;

/* read a default file to work out values of m and n */

/* now dynamically allocate memory block for data and index */

data = new double * [m];
for (int j=0; j<m; j++) data[j] = new double [n];

index = new int [m];

/* read a default file to initialize data and index */

* data_ = data;
* index_ = index;
}

};

My questions here are:

It looks like ClassB is not responsible for deleting the memoey block dynamically allocated for data and index within its member function loadData. Do I need to delete the memory explicitly in ClassA ? I saw some sample code without removing such kind of memory explicitly, but all C++ references have mentioned that any memory allocated in heap with new must be freed with delete. I was confused. If ClassA does not free this memory explicitly, can the memory be removed automatically after outscoping ClassA or eventually after application ends ? Someone told me it is compiler dependent, some compilers can handle automatic removing, but some do not. I am wondering which compilers can deal with this. What are better ways to get rid of memory leak?


The second scenery of usage of 'dynamic memory allocation': Although memory allocated using new is usually in heap, it is possible to allocate memory in static storage. Sample code is listed here:

class SimpleClass {
//...
public:
SimpleClass(int);
~SimpleClass( );
//...
};

/* A global operator new is overrided here */
void * operator new (size_t, void *p) { return p; }

/* We now allocate memory using new for a reserved memory block */

char buffer [sizeof (SimpleClass) ];

void func(int i) {
SimpleClass * p = new (buffer) SimpleClass (i);
//...
}

I understand buffer is not in heap, my question is:

While operator new 'allocates' memory for buffer ( or initiates a SimpleClass object in this memory more exactly ) , does operator new allocate a dynamic memory in heap as well ?


Please give me some comments on my questions. Any help would be appropriated.


best regards,

Jianhua
 
My linux pc will go and search for unused reserved memory when he runs out of ram.However, you should always free your memory before you exit your program !
You should write the code in your destructor,so it will free the memory when the object goes out of scope.

You can trace leaks with 'top' ,I run top in one window,and my application in another. That way I can keep an eye on possible leaks.

The memory should be freed in class A,with the same loop you used to create the array (but then of course with deletes :)). The Muppeteer.

themuppeteer@hotmail.com

Don't eat yellow snow...
 
thoughts regarding the 2-nd question:
The overridden operator &quot;new&quot; does not allocate dynamic memory (i'm in doubt with your syntax, i'm not familiar with operator overridding).
The main rule: if you override operator &quot;new&quot;, you should override operator &quot;delete&quot; aswell.
 
Thanks to you for your comments and time.

For part one, ClassA needs to check memory release in its destructor - it is clear now;

For part two, I saw &quot;placement&quot; operator new concept, and I was wondering what's its effect on memory allocation.

Thanks again,

Jianhua
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top