Hi everyone,
I have a problem with large dynamic memory allocating. this is my code (in briefness):
The runtime error occurs when program releases memory that is allocated to "Vec_".
This program gets 1GB of memory approximately, while the 32bits systems can use at most 4GB of memory.
if I reduce the value of "Adim" or "udim", program will get less memory, and there will be no error.
However I need to set these large values to both "Adim" and "udim".
My compliler is Borland C++ 5.02.
I will appreciate, if you help me to overcome this problem.
Thanks.
I have a problem with large dynamic memory allocating. this is my code (in briefness):
Code:
#include <stdio.h>
#include <mem.h>
#include <assert.h>
#define SML_BOUNDS_CHECK
class Vector_d
{
private:
int Dim_;
double* Val_;
public:
Vector_d() { Dim_ = 0; Val_ = NULL; }
~Vector_d() { delete[] Val_; }
int SetDim( int N ); // Get memory for Val_
};
class SVector_d
{
private:
int NNZ_;
double* Val_;
int* Ind_;
public:
SVector_d() { Val_ = NULL; Ind_ = NULL; NNZ_ = 0; };
~SVector_d() { delete[] Val_; delete[] Ind_; };
int SetNNZ( int NNZ ); // Get memory for Val_ and Ind_
int& Index( int NZI );
double& Value( int NZI );
SVector_d& operator=( const SVector_d& V );
friend class SMatrix_d;
};
class SMatrix_d
{
private:
int NVec_;
SVector_d* Vec_;
public:
SMatrix_d() { NVec_ = 0; Vec_ = NULL; };
~SMatrix_d() { delete[] Vec_; };
int SetNVec( int NV );
SVector_d& operator()( int Index );
};
int Vector_d::SetDim( int N )
{
if(N==Dim_) return 0;
delete[] Val_;
Val_ = new double[N];
Dim_ = N;
return 0;
}
int SVector_d::SetNNZ( int NNZ )
{
if(NNZ==NNZ_) return 0;
delete[] Val_;
Val_ = new double[NNZ];
delete[] Ind_;
Ind_ = new int[NNZ];
NNZ_ = NNZ;
return 0;
}
int& SVector_d::Index( int NZI )
{
#ifdef SML_BOUNDS_CHECK
assert( 0<=NZI && NZI<NNZ_ );
#endif
return Ind_[NZI];
}
double& SVector_d::Value( int NZI )
{
#ifdef SML_BOUNDS_CHECK
assert( 0<=NZI && NZI<NNZ_ );
#endif
return Val_[NZI];
}
SVector_d& SVector_d::operator=( const SVector_d& V )
{
if (this == & V) return *this;
SetNNZ( V.NNZ_ );
memcpy( Val_ , V.Val_ , sizeof(double)*NNZ_ );
memcpy( Ind_ , V.Ind_ , sizeof(int)*NNZ_ );
return *this; // General call
}
int SMatrix_d::SetNVec( int NV )
{
if(NV==NVec_) return 0;
delete[] Vec_;
Vec_ = new SVector_d[NV];
NVec_ = NV;
return 0;
}
SVector_d& SMatrix_d::operator()( int Index )
{
#ifdef SML_BOUNDS_CHECK
assert( 0<=Index && Index<NVec_ );
#endif
return Vec_[Index];
}
int main() {
int Adim = 7000000;
int udim = 1000000;
printf("A dimension = %d\n",Adim);
printf("u dimension = %d\n",udim);
Vector_d u;
u.SetDim( udim );
SMatrix_d A;
A.SetNVec( Adim );
SVector_d SV;
SV.SetNNZ(5);
for(int i=0; i<5; i++) { SV.Value(i) = i; SV.Index(i)=i; };
for(int i=0; i<Adim; i++ ) A(i) = SV;
A.SetNVec( 20 ); // Error occurs at this place
A(0) = SV;
printf("Done!\n");
return 0;
} // End of program
This program gets 1GB of memory approximately, while the 32bits systems can use at most 4GB of memory.
if I reduce the value of "Adim" or "udim", program will get less memory, and there will be no error.
However I need to set these large values to both "Adim" and "udim".
My compliler is Borland C++ 5.02.
I will appreciate, if you help me to overcome this problem.
Thanks.
Comment