If I define a custom destructor, do I have to delete every variable manually?
Memory allocated by malloc
should be free
d in the destructor. How about the pointer to the memory allocated by malloc
, and an int
?
a.h:
#ifndef A_H
#define A_H
#include <stdlib.h>
#include <iostream>
#include <stdint.h>
using namespace std;
class A{
public:
uint32_t x;
uint32_t* ar_y;
A(void);
~A(void);
};
#endif
a.cpp:
#include "a.h"
A::A(void){
x = 0;
ar_y = (uint32_t*)(malloc(4));
}
A::~A(void){
// free the memory allocated by malloc
free(ar_y);
//Is it ok to do nothing for int* y and int x?
}
test.cpp:
#include "a.h"
int f(void){
A objA;
//cout << objA.x << endl;
//Upon exiting the function
//destructor of A is called.
}
int main(void){
uint32_t i;
// see if memory usage go crazy.
for (i = 0; i < 10000000000; i++) f();
}
Test result:
memory usage didn't rise crazily.