Hi all,

I am a beginner to coding. Forgive me if my question is too silly. What I would like to know is can dynamic allocation in a loop result in shortage of memory. what I am trying to do is find FFT of a large no of images in a loop. For all the computation storage I make use of dynalically allocated arrays in C++ [malloc]. I define my dynamic arrays outside the loop before I start the computations. I am able to do the same only for a few no of images [around 40] and not for more than that. After 40, it says run out of memory. Could somebody tell what my fundamental mistake is?

My code is as shown below..

``````//FFTW complex Allocation
fftw_complex *H_r,*H4,*R,*HF,*FF, *RES,*Ud;
H_r = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*lWidth*lHeight);
H4 = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*lWidth*lHeight);
FF = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*lWidth*lHeight);
HF = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*lWidth*lHeight);//DYNAMIC
RES = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*lWidth*lHeight);//DYNAMIC
R = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*lWidth*lHeight);
Ud = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*lWidth*lHeight);

//Dynamic Allocation[Double]
double* I = (double*)malloc(lWidth*lHeight*sizeof(double));
double *tmpData = (double*)malloc(lWidth*lHeight*sizeof(double));
unsigned char* rData;
rData=(unsigned char*)malloc(lWidth*lHeight*sizeof(unsigned char));
double max,min;
max = *I;
min = *I;
double hfa,hfb,wfa,wfb;

for(j=0;j<lHeight;j++)
{	for(i=0;i<lWidth;i++)
{   H_r[j*lWidth+i][0]=fftwIn[j*lWidth+i][                  0]*UR[j*lWidth+i][0];
H_r[j*lWidth+i][1]=fftwIn[j*lWidth+i][0]*UR[j*lWidth+i][1];
}
}
FourierFoward(H_r,FF,lWidth,lHeight);
FFTShift(FF,H4, lWidth, lHeight);

for(int slice_no=0;slice_no<44;slice_no++)
{
float sd= (slice_depths[slice_no]*100000);
for(j=0;j<lHeight;j++)
{
for(i=0;i<lWidth;i++)
{
tmp = k*d[slice_no]*G[j*lWidth+i];
HF[j*lWidth+i][0] = cos(k*d[slice_no]*G[j*lWidth+i]);
HF[j*lWidth+i][1] = sin(k*d[slice_no]*G[j*lWidth+i]);
}
}

for(j=0;j<lHeight;j++)
{
for(i=0;i<lWidth;i++)
{
hfa=H4[j*lWidth+i][0];
hfb=H4[j*lWidth+i][1];
wfa=HF[j*lWidth+i][0];
wfb=HF[j*lWidth+i][1];
RES[j*lWidth+i][0]= hfa*wfa-hfb*wfb;;
RES[j*lWidth+i][1]= hfa*wfb+hfb*wfa;;
}
}

FFTShift(RES, R, lWidth,lHeight);
FourierBackward(R,Ud,lWidth,lHeight);

for(j=0;j<lHeight;j++)
{
for(i=0;i<lWidth;i++)
{
hfa = Ud[j*lWidth+i][0];
hfb = Ud[j*lWidth+i][1];
wfa=R[j*lWidth+i][0];
wfb=R[j*lWidth+i][1];
I[j*lWidth+i] = hfa*hfa+hfb*hfb;
}
}

//pDoc->m_I = I;

for(j=0;j<lHeight;j++)
{
for(i=0;i<lWidth;i++)
{
tmp = I[j*lWidth+i];
if(max<tmp)
max = tmp;
if(min>tmp)
min = tmp;
}
}

for(j=0;j<lHeight;j++)
{
for(i=0;i<lWidth;i++)
{
rData[j*lWidth+i] = (unsigned char)((255.0*(I[j*lWidth+i]-min))/(max-min));
//lpSrc = (unsigned char*)lpDIBBits + lLineBytes * (lHeight - 1 - j) + i;
//	* (lpSrc) = (BYTE)(rData[j*lWidth+i]);
}
}
//pDoc->UpdateAllViews(NULL);
//::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());

string folderName = "Images";
string fileName   = folderName+ "\\Holo_";
std::ostringstream index;
index <<sd;
index.seekp(0);
fileName += index.str() + ".bmp";
SaveGrayBitmap(fileName.c_str(),lWidth,lHeight,rData);

EndWaitCursor();
}``````

## All 5 Replies

If you don't free() the memory before leaving that function then yes, there will be memory leak.

In c++ program you should use new and delete[] instead of malloc() and free().

I don't see where you're allocating memory in a loop, but yes, dynamically allocating memory in a loop can cause you to quickly run out of memory if you don't remember to free the memory inside the loop. C/C++ has no garbage collector. Unlike non-dynamic memory, you don't get the memory back when the variable goes out of scope. The pointer may go out of scope, but that doesn't free the memory. You often end up with reserved memory with no pointer to access it, which is a memory leak. I see no "free" command in your code.

I have free the memory outside the loop before exiting my function....but when I am running this whole function, before it completes the entire loop, it runs out of memory....here the loop i refer to is(int slice_no=0;.....)

I have free the memory outside the loop before exiting my function....but when I am running this whole function, before it completes the entire loop, it runs out of memory....here the loop i refer to is(int slice_no=0;.....)

``````/////DECLARATIONS AND DYNAMIC ALLOCATIONS

//FFTW complex Allocation
fftw_complex *H_r,*H4,*R,*HF,*FF, *RES,*Ud;
H_r = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*lWidth*lHeight);
H4 = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*lWidth*lHeight);
FF = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*lWidth*lHeight);
HF = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*lWidth*lHeight);//DYNAMIC
RES = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*lWidth*lHeight);//DYNAMIC
R = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*lWidth*lHeight);
Ud = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*lWidth*lHeight);
//Dynamic Allocation[Double]

double* I = (double*)malloc(lWidth*lHeight*sizeof(double));
double *tmpData = (double*)malloc(lWidth*lHeight*sizeof(double));
unsigned char* rData;
rData = (unsigned char*)malloc(lWidth*lHeight*sizeof(unsigned char));
double max,min;
max = *I;
min = *I;
double hfa,hfb,wfa,wfb;

for(j=0;j<lHeight;j++)
{	for(i=0;i<lWidth;i++)
{   H_r[j*lWidth+i][0]=fftwIn[j*lWidth+i][0]*UR[j*lWidth+i][0];
H_r[j*lWidth+i][1]=fftwIn[j*lWidth+i][0]*UR[j*lWidth+i][1];
}
}
FourierFoward(H_r,FF,lWidth,lHeight);
FFTShift(FF,H4, lWidth, lHeight);

for(int slice_no=0;slice_no<44;slice_no++)
{
float sd= (slice_depths[slice_no]*100000);
for(j=0;j<lHeight;j++)
{
for(i=0;i<lWidth;i++)
{
tmp = k*d[slice_no]*G[j*lWidth+i];
HF[j*lWidth+i][0] = cos(k*d[slice_no]*G[j*lWidth+i]);
HF[j*lWidth+i][1] = sin(k*d[slice_no]*G[j*lWidth+i]);
}
}

for(j=0;j<lHeight;j++)
{
for(i=0;i<lWidth;i++)
{
hfa=H4[j*lWidth+i][0];
hfb=H4[j*lWidth+i][1];
wfa=HF[j*lWidth+i][0];
wfb=HF[j*lWidth+i][1];
RES[j*lWidth+i][0]= hfa*wfa-hfb*wfb;;
RES[j*lWidth+i][1]= hfa*wfb+hfb*wfa;;
}
}

FFTShift(RES, R, lWidth,lHeight);
FourierBackward(R,Ud,lWidth,lHeight);

for(j=0;j<lHeight;j++)
{
for(i=0;i<lWidth;i++)
{
hfa = Ud[j*lWidth+i][0];
hfb = Ud[j*lWidth+i][1];
wfa=R[j*lWidth+i][0];
wfb=R[j*lWidth+i][1];
I[j*lWidth+i] = hfa*hfa+hfb*hfb;
}
}

//pDoc->m_I = I;

for(j=0;j<lHeight;j++)
{
for(i=0;i<lWidth;i++)
{
tmp = I[j*lWidth+i];
if(max<tmp)
max = tmp;
if(min>tmp)
min = tmp;
}
}

for(j=0;j<lHeight;j++)
{
for(i=0;i<lWidth;i++)
{
rData[j*lWidth+i] = (unsigned char)((255.0*(I[j*lWidth+i]-min))/(max-min));
//lpSrc = (unsigned char*)lpDIBBits + lLineBytes * (lHeight - 1 - j) + i;
//	* (lpSrc) = (BYTE)(rData[j*lWidth+i]);
}
}
//pDoc->UpdateAllViews(NULL);
//::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());

string folderName = "ReconstructedImages";
string fileName   = folderName+ "\\Hologram_";
std::ostringstream index;
index <<sd;
index.seekp(0);
fileName += index.str() + ".bmp";
SaveGrayBitmap(fileName.c_str(),lWidth,lHeight,rData);

EndWaitCursor();
}

fftw_free(HF);
fftw_free(RES);
fftw_free(R);
fftw_free(Ud);
fftw_free(FF);
fftw_free(UR);
fftw_free(H_r);
fftw_free(fftwIn);
fftw_free(w);
fftw_free(H4);
delete[] tmpData;
delete[] rData;
delete[] I;
delete[] G;``````

If you don't free() the memory before leaving that function then yes, there will be memory leak.

In c++ program you should use new and delete[] instead of malloc() and free().

I have free the memory outside the loop before exiting my function....but when I am running this whole function, before it completes the entire loop, it runs out of memory....here the loop i refer to is(int slice_no=0;.....)

``````/////DECLARATIONS AND DYNAMIC ALLOCATIONS

//FFTW complex Allocation
fftw_complex *H_r,*H4,*R,*HF,*FF, *RES,*Ud;
H_r = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*lWidth*lHeight);
H4 = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*lWidth*lHeight);
FF = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*lWidth*lHeight);

RES = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*lWidth*lHeight);//DYNAMIC
R = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*lWidth*lHeight);
Ud = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*lWidth*lHeight);
//Dynamic Allocation[Double]

for(j=0;j<lHeight;j++)
{	for(i=0;i<lWidth;i++)
{   H_r[j*lWidth+i][0]=fftwIn[j*lWidth+i][0]*UR[j*lWidth+i][0];
H_r[j*lWidth+i][1]=fftwIn[j*lWidth+i][0]*UR[j*lWidth+i][1];
}
}
FourierFoward(H_r,FF,lWidth,lHeight);
FFTShift(FF,H4, lWidth, lHeight);

for(int slice_no=0;slice_no<44;slice_no++)
{
//int slice_no=0;
float sd= (slice_depths[slice_no]*1000000);

for(j=0;j<lHeight;j++)
{
for(i=0;i<lWidth;i++)
{
tmp = k*d[slice_no]*G[j*lWidth+i];
HF[j*lWidth+i][0] = cos(k*d[slice_no]*G[j*lWidth+i]);
HF[j*lWidth+i][1] = sin(k*d[slice_no]*G[j*lWidth+i]);
}
}

double hfa,hfb,wfa,wfb;

for(j=0;j<lHeight;j++)
{
for(i=0;i<lWidth;i++)
{
hfa=H4[j*lWidth+i][0];
hfb=H4[j*lWidth+i][1];
wfa=HF[j*lWidth+i][0];
wfb=HF[j*lWidth+i][1];
RES[j*lWidth+i][0]= hfa*wfa-hfb*wfb;;
RES[j*lWidth+i][1]= hfa*wfb+hfb*wfa;;
}
}

FFTShift(RES, R, lWidth,lHeight);
FourierBackward(R,Ud,lWidth,lHeight);

double* I = (double*)malloc(lWidth*lHeight*sizeof(double));
for(j=0;j<lHeight;j++)
{
for(i=0;i<lWidth;i++)
{
hfa = Ud[j*lWidth+i][0];
hfb = Ud[j*lWidth+i][1];
wfa=R[j*lWidth+i][0];
wfb=R[j*lWidth+i][1];
I[j*lWidth+i] = hfa*hfa+hfb*hfb;
//	lpSrc = (unsigned char*)lpDIBBits + lLineBytes * (lHeight - 1 - j) + i;
//* (lpSrc) = (BYTE)(I[j*lWidth+i]);
}
}

//	pDoc->UpdateAllViews(NULL);
//	::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());

pDoc->m_I = I;
//	pDoc->m_ph = ph;

double max,min;
max = *I;
min = *I;
for(j=0;j<lHeight;j++)
{
for(i=0;i<lWidth;i++)
{
tmp = I[j*lWidth+i];
if(max<tmp)
max = tmp;
if(min>tmp)
min = tmp;
}
}

unsigned char* rData;
rData = (unsigned char*)malloc(lWidth*lHeight*sizeof(unsigned char));
for(j=0;j<lHeight;j++)
{
for(i=0;i<lWidth;i++)
{
//	rData[j*lWidth+i] = (unsigned char)I[j*lWidth+i];
rData[j*lWidth+i] = (unsigned char)((255.0*(I[j*lWidth+i]-min))/(max-min));
//	lpSrc = (unsigned char*)lpDIBBits + lLineBytes * (lHeight - 1 - j) + i;
//			* (lpSrc) = (BYTE)(rData[j*lWidth+i]);
}
}

free (I);
//pDoc->UpdateAllViews(NULL);
//	::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
//string distance[pDoc->m_DepthSteps];

string folderName = "ReconstructedImages";
string fileName   = folderName+ "\\Hologram_";
std::ostringstream index;
index <<sd;
index.seekp(0);
fileName += index.str() + ".bmp";
SaveGrayBitmap(fileName.c_str(),lWidth,lHeight,rData);

free (rData);

//delete[] G;

//	::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
EndWaitCursor();
}``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.