Sign in to follow this  
staticVoid2

extending dynamic memory

Recommended Posts

hi, I was wondering if there is anyway to extend an allocated array(in c++) without having to copy all the contents from one to the other to save references to the original array from being lost. eg.
UINT size = 50;
UINT current = 0;
int *array = new int[size];

void add(int a)
{
   if(current++ == size)
   {
      int *temp = new int[size*2];
      copyarray(array, temp);
      delete[] array;
      array = temp;
   }
}


any pointers to the original array will be lost after you create the new array, Is there any way around this?

Share this post


Link to post
Share on other sites
how about using STL

int main(blabla)
{
std::vector<mypointer*> vec;

mypointer* p = new mypointer;
vec.push_back( p );

return 0;
}


now you can keep on adding without having to think about moving or reservering memory. The vector will resize itself when it needs more memory. Of course if you are planning on doing this in realtime then you should use the reserve method on the vector.

hope this helps,
Peter Wraae Marino

Share this post


Link to post
Share on other sites
Quote:
Original post by staticVoid2
any pointers to the original array will be lost after you create the new array, Is there any way around this?

Depends on exactly what the pointers point to.
If they simply point to the array, use a std::vector instead of raw arrays.
If they point into the array, to specific data elements, no. You'll have to use a different data structure if you want those to stay valid.

Share this post


Link to post
Share on other sites
Quote:
Original post by staticVoid2
hi, I was wondering if there is anyway to extend an allocated array(in c++) without having to copy all the contents from one to the other to save references to the original array from being lost.


If you use std::vector<> it takes care of the copying for you. It also helps to reduce the cost of copying by allocating more than you need initially.

Quote:
any pointers to the original array will be lost after you create the new array, Is there any way around this?


Use a different data structure, such as a linked list (std::list<>), or use a smart pointer that refers to an index of an existing std::vector<>, something like:


// completely untested

template<typename T>
class vec_elem_ptr
{
public:
typedef std::vector<T>::size_type index_type;

vec_elem_ptr(std::vector<T> &v, index_type idx) :
v_(&v),
idx_(idx)
{
}

T &operator* () const { return v_->at(idx_); } // or assert() and []
T *operator-> () const { return &v_->at(idx_); } // ditto

// etc ...
// operators ++, -- etc would probably make sense here, too

private:
std::vector<T> *v_;
index_type idx_;
};

// Maybe a partial specialisation for <const T>...


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this