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();
}
Thanks in advance...