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
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