Jump to content
  • Advertisement
Sign in to follow this  
staticVoid2

extending dynamic memory

This topic is 3773 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!