Re: new char[480000000] crashes



This is my working solution to work with big tables in case not enough contiguous memory. Any suggestions would be appreciated.

Thanks

<CODE>

/*************************************************************************
* START: class HokusVector
*************************************************************************/

template<class T>
class HokusVector
{

// typedefs
//

public:
typedef T Value;



// konstruktory / destruktory
//

public:
HokusVector();
~HokusVector();


// operatory // public: Value& operator[](const unsigned int& i); const Value& operator[](const unsigned int& i) const;


// funkcje uzytkowe // public: void reserve( unsigned int size ); unsigned int size() const; bool empty() const; void clear(); void resize( unsigned int size ); void push_back( const T& value );


// dane klasy // private: std::vector< std::vector<T> > m_banks; unsigned int m_banksize;

};

/*************************************************************************
* END: class HokusVector
*************************************************************************/





/*************************************************************************
* START: class HokusVector
*************************************************************************/

//\\//\\//\\//\\//\\//\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
template<class T>
HokusVector<T>::HokusVector()
{ // set
m_banksize = __max( 10, 20000000/sizeof(T) );
}


//\\//\\//\\//\\//\\//\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// template<class T> HokusVector<T>::~HokusVector() { //

}


//\\//\\//\\//\\//\\//\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// template<class T> HokusVector<T>::Value& HokusVector<T>::operator[](const unsigned int& i) { // success return m_banks[i/m_banksize][i-(i/m_banksize)*m_banksize]; }


//\\//\\//\\//\\//\\//\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// template<class T> const HokusVector<T>::Value& HokusVector<T>::operator[](const unsigned int& i) const { // success return m_banks[i/m_banksize][i-(i/m_banksize)*m_banksize]; }


//\\//\\//\\//\\//\\//\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// template<class T> void HokusVector<T>::reserve( unsigned int size ) { // test if( m_banks.size()*m_banksize >= size ) { // ERROR: nothing to reserve return; }

// resize vectors
m_banks.resize( (size-1 )/m_banksize +1 );

// reserve all vectors
for( unsigned int i=0 ; i<m_banks.size() ; ++i )
{ //
m_banks[i].reserve( m_banksize );
}

}


//\\//\\//\\//\\//\\//\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// template<class T> unsigned int HokusVector<T>::size() const {

// variables
unsigned int
i, size=0;

// calc
for( i=0 ; i<m_banks.size() ; ++i )
{ //
size += m_banks[i].size();
}

// success
return size;

}


//\\//\\//\\//\\//\\//\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// template<class T> bool HokusVector<T>::empty() const { // test for( unsigned int i=0 ; i<m_banks.size() ; ++i ) { // if( !m_banks[i].empty() ) { // ERROR: not empty return false; }

}

// success: empty
return true;

}


//\\//\\//\\//\\//\\//\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// template<class T> void HokusVector<T>::clear() { // clear for( unsigned int i=0 ; i<m_banks.size() ; ++i ) { // m_banks[i].clear(); }

// clear
m_banks.clear();

}


//\\//\\//\\//\\//\\//\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// template<class T> void HokusVector<T>::resize( unsigned int size ) { // test if( 0 == size ) { // just clear return clear(); }

// resize vectors
m_banks.resize( (size-1)/m_banksize +1 );

// resize all vectors
for( unsigned int i=0 ; i<m_banks.size()-1 ; ++i )
{ //
m_banks[i].resize( m_banksize );
}

// resize last vector
m_banks[m_banks.size()-1].reserve( m_banksize );
m_banks[m_banks.size()-1].resize( size - (m_banks.size()-1)*m_banksize );

}


//\\//\\//\\//\\//\\//\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// template<class T> void HokusVector<T>::push_back( const HokusVector<T>::Value& value ) { // get size unsigned int newsize = this->size() +1;

// calc nvectors
unsigned int ivector = (newsize-1) / m_banksize;

// add new vector
if( m_banks.size() <= ivector )
{
//
m_banks.push_back( std::vector<T>() );
m_banks.back().reserve( m_banksize );
assert( m_banks.size() == ivector+1 );
}

// assert
assert( !m_banks.empty() );

// add new item
m_banks[ivector].push_back( value );
assert( m_banks[ivector].size() <= m_banksize );

}


/************************************************************************* * END: class HokusVector *************************************************************************/

</CODE>

.



Relevant Pages