How do I remove variables from the heap?
I have been doing some research and still cannot find a solution to my problem. As far as I know, when we declare variables outside the function, they are allocated inside the heap, and this memory is not released until the end of execution; unless we specifically do it with a function delete
. I tried the following features to make variables declared in the beginning of the code, and none of them did not work (getting debugging errors dbgdel.cpp): delete
, delete []
, free()
. What am I doing wrong?
I'll embed below the summarized version of the code. Any help is appreciated. Thank!
(I know that globals are usually not desirable when programmed correctly, but this is not my piece of code, I'm just trying to fix it as it is.)
#include <stdio.h>
#include <conio.h>
#include <cv.h>
#include <highgui.h>
#include <cxcore.h>
#include "Viewer.h"
....
// Arrays
float z_base [5201][5201];
....
uchar maskThreshold [5200][5200];
...
void main(){
.....
delete [] z_base;
//free (z_base);
//delete z_base;
//free (&z_base);
}
As far as I know, when I declare variables outside of each function, they are allocated inside the heap
This is not true. In general, you only need to call delete
or delete[]
if you allocated memory using new
or new []
respectively.
- There is no heap in C ++.
- All memory is freed at the end of execution.
-
delete
what are younew
,delete[]
what are younew[]
. -
void main
is incorrect (main must return int).
That's all.
Not. The runtime will do it for you. Typically, you only need the delete/delete[]
one you have highlighted with new/new[]
.
Also note that globals are not allocated on the heap, but in static memory.