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!

dynamic memory allocation

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 i=0; i<m; i++) data = 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
 
1. The scope of ClassB in ClassA is only in the function init.

2. the code for the dynamic memory allocation has been italicized. Use the code tags. I am assuming

Code:
data[i] = new double[n]
???

Class A owns the types and should have a check in its destructor

Code:
if(data)
   delete data;

if(index)
   delete index;


For part 2, i see no need to overload the &quot;new&quot; operator and as general practice I avoid overloading new.

you will achieve the same result by either

Code:
char buffer[sizeof(simpleClass)];
SimpleClass s;
memcpy(buffer,s,sizeof(s));

or just 

return new SimpleClass;

Matt





 
Thanks for your comments, Matt.

For part one, I got the idea from your comment; for part 2, your way is more elegant. I am still wondering if I overloaded 'new' that way, what would happen with memory allocation in heap.

regards,

Jianhua
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top