Reputation: 1
I used to work with 3D dynamic allocated arrays but I read that 1D array has better performance so I tried to transorm my 3D array in a 1D array.
I work with two big arrays (about 240*320*1000 double) and the allocation look like this :
int numberFiles; //about 1000, can change
int nRow; //240
int nCol; //320
double *inputData;
double *outputData;
inputData = (double*)malloc(nCol*nRow*numberFiles*sizeof(double)); //Always works
outputData = (double*)malloc(nCol*nRow*numberFiles*sizeof(double)); //Always fails
The second allocation always fail. Since the first doesn't, it's not a size problem. And since, when allocate this in a 3D arrays, it's also working, it's not a memory space problem
Any ideas why the second allocation fails ?
Thanks.
Chen Song.
PS : Sorry for the poor english.
PS2: Tried new Double[], same problems.
Edit : So allocation in a simple code works but as soon as I try to put it in my app, it's not working anymore. I'm woking with QtCreator with mingw32 and have matio(libraries to read MATLAB files) to read data. Here a code example of my program which give me error :
//Needed include
using namespace std;
int main(int argc, char ** argv)
{
int k,i,j;
double *matriceBrute,*matriceSortie;
double* matData;
const char * c;
mat_t *matfp;
matvar_t *matvar;
QApplication app(argc,argv);
// Getting the directory of the data and the files in it
QString directoryPath = QFileDialog::getExistingDirectory();
QDir myDir(directoryPath);
myDir.setNameFilters(QStringList()<<"*.MAT");
QStringList filesList = myDir.entryList(QDir::Files | QDir::Hidden);
string fullPath= (directoryPath+"/"+filesList[0]).toLocal8Bit().constData();
c = fullPath.c_str();
// Loading the first one to get the size of the data
matfp = Mat_Open(c,MAT_ACC_RDONLY);
matvar = Mat_VarReadNextInfo(matfp);
int read_err = Mat_VarReadDataAll(matfp,matvar);
int numberFiles = (int)filesList.size();
int nCol = matvar->dims[1];
int nRow = matvar->dims[0];
//Allocating
matriceBrute = (double*)malloc(nCol*nRow*numberFiles*sizeof(double));
matriceSortie = (double*)malloc(nCol*nRow*numberFiles*sizeof(double));
//the first one works but not the second
//loading the data in the allocated memory
for (k=0;k<numberFiles;k++)
{
fullPath= (directoryPath+"/"+filesList[k]).toLocal8Bit().constData();
c = fullPath.c_str();
matfp = Mat_Open(c,MAT_ACC_RDONLY);
matvar = Mat_VarReadNext(matfp);
matData = (double*)(matvar->data);
for (i=0;i<nRow;i++)
{
for (j=0;j<nCol;j++)
{
matriceBrute[i + nRow*(j+k*nCol)]=*(matData+(i+j*nRow));
matriceSortie[i + nRow*(j+k*nCol)]=*(matData+(i+j*nRow));
}
}
Mat_VarFree(matvar);
Mat_Close(matfp);
}
}
Upvotes: 0
Views: 166
Reputation: 2207
May it be memory fragmentation problem? If, for example, you can't alloc ~0.6Gb (by one heap) twice? First time you alloc it, at second - it just can't find in your physical memory such big heap of free memory? Which exception do you receive?
Upvotes: 1
Reputation: 7118
No error, it works.
#include <iostream>
using namespace std;
int main()
{
int numberFiles = 1000; //about 1000, can change
int nRow = 240; //240
int nCol = 240; //320
double *inputData;
double *outputData;
inputData = (double*)malloc(nCol*nRow*numberFiles*sizeof(double)); //Always works
outputData = (double*)malloc(nCol*nRow*numberFiles*sizeof(double)); //Always fails
if (inputData == NULL)
cout << "inputData alloc failed" << endl;
if (outputData == NULL)
cout << "outputData alloc failed" << endl;
return 0;
}
It does not print as failed, so, don't worry, be happy.
Upvotes: 1