theotick
November 29th, 2007, 09:44 PM
I'm new to linux and learning C++. I'm working on a project that requires a dynamic two dimensional array. I wrote the following class but when I compile (compiles fine) and run I can't allocate an array larger than 5x5( 25 integers):
class vectorArray{
private:
int nRow, nCol;
int * localArray;
public:
int pRow, pCol;
int arraySize;
vectorArray( int, int );
~vectorArray();
int accessArray( int, int);
void modifyArray( int, int, int );
};
vectorArray::vectorArray( int a, int b ){
localArray = new (nothrow) int[nRow*nCol];
if( localArray == 0 ){
cout << "Memory not allocated:: Exiting"<< endl;
exit(1);
}
nRow = a;
nCol = b;
pRow = nRow;
pCol = nCol;
arraySize = (nRow*nCol)-1;
}
int vectorArray::accessArray( int arow, int acolumn){
return( localArray[(arow*nCol)+acolumn] );
}
void vectorArray::modifyArray( int arow, int acolumn, int nvalue){
localArray[(arow*nCol)+acolumn] = nvalue;
}
vectorArray::~vectorArray(){
delete localArray;
}
The laptop I'm using isn't a powerhouse but free -m shows 40mb of free memory so shouldn't I be able to allocate up to 10million integers? Can anyone help?
class vectorArray{
private:
int nRow, nCol;
int * localArray;
public:
int pRow, pCol;
int arraySize;
vectorArray( int, int );
~vectorArray();
int accessArray( int, int);
void modifyArray( int, int, int );
};
vectorArray::vectorArray( int a, int b ){
localArray = new (nothrow) int[nRow*nCol];
if( localArray == 0 ){
cout << "Memory not allocated:: Exiting"<< endl;
exit(1);
}
nRow = a;
nCol = b;
pRow = nRow;
pCol = nCol;
arraySize = (nRow*nCol)-1;
}
int vectorArray::accessArray( int arow, int acolumn){
return( localArray[(arow*nCol)+acolumn] );
}
void vectorArray::modifyArray( int arow, int acolumn, int nvalue){
localArray[(arow*nCol)+acolumn] = nvalue;
}
vectorArray::~vectorArray(){
delete localArray;
}
The laptop I'm using isn't a powerhouse but free -m shows 40mb of free memory so shouldn't I be able to allocate up to 10million integers? Can anyone help?