[英]Memory allocation c++
I have a problem with memory allocation, when running the following while loop the occupied RAM of my computer continue to increase, I don't understand what I have to allocate and how to do it 我的内存分配有问题,在运行以下while循环时,我的计算机占用的RAM继续增加,我不知道该分配什么以及如何做。
while(cvWaitKey(1)<0)
{
img = cvQueryFrame(capture);
height = img->height;
width = img->width;
step = img->widthStep;
channels = img->nChannels;
gray_arr = (uchar * * )malloc(sizeof(uchar * )*(height + 1));
for(int i=0; i<height; i++)
{
gray_arr[i] = (uchar * )malloc(sizeof(uchar)*(width + 1));
for(int j=0; j<width; j++)
{
gray_arr[i][j] = (0.11*data[i*step + j*channels] + 0.56*data[i*step + j*channels + 1] + 0.33*data[i*step + j*channels + 2]);
}
}
for(int j=0; j<width; j++)
{
sum=0;
for(int i=first_pixel; i<last_pixel+1; i++)
{
value= gray_arr[i][j];
sum = sum + value;
}
sum=sum/sample_number;
store[j]= sum;
}
for(int i=0; i<width; i++)
{
intensity_inv[i]=270- store[i];
}
graph = cvCreateImage(cvSize(1280,300),IPL_DEPTH_8U,1);
cvRectangle(graph, cvPoint(256,0), cvPoint(255,1), CV_RGB(255,255,255),1);
for (int i=0; i<width; i++)
{
cvLine(graph,cvPoint(i,270),cvPoint(i,intensity_inv[i]),CV_RGB(0,0,0));
}
cvNamedWindow("Sp",CV_WINDOW_AUTOSIZE);
cvMoveWindow("Sp",0,550);
cvNamedWindow("Original Image", CV_WINDOW_NORMAL);
cvMoveWindow("Original Image", 500, 0);
cvShowImage("Original Image", img);
cvShowImage("Sp",graph);
free(gray_arr);
}
Sorry for my bad english 对不起,我的英语不好
Thanks 谢谢
You're calling malloc for the gray_arr
array: 您正在为gray_arr
数组调用malloc:
gray_arr = (uchar * * )malloc(sizeof(uchar * )*(height + 1));
and also for elements within that array: 以及该数组中的元素:
gray_arr[i] = (uchar * )malloc(sizeof(uchar)*(width + 1));
However, you're only calling free
for the allocation of the entire array. 但是,您只需要为整个数组的分配调用free
。 You need to also free up the allocation of each element. 您还需要释放每个元素的分配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.