Pointer to pointer?

Not sure if I'm doing this right. Do I really need to create a separate pointer to point to dynamic arrays?

1
2
3
4
5
6
7
T* temp = new T[capacity_ * 2];
        T* tIter = &temp; //Do these need to be here?
        T* dIter = &data; //Or can I use *(temp + i) and *(data + i)?
        (for unsigned int i = 0; i < size_; i++)
        {
            *(tIter + i) = *(dIter + i);
        }
You can use the original pointers.
closed account (o1vk4iN6)
Is this for your vector class ? I would do something a bit differently:

1
2
3
4
5
6
7
// to avoid default constructor
T* temp = reinterpret_cast<T*>( operator new( sizeof(T) * capacity_ * 2 ) );

for( unsigned i = 0; i < size_; ++i )
{ 
    new( &temp[i] ) T( std::move( data[i] ) );
} 


Didn't check it but the problem with using new directly like that is that it calls the default constructor which might be expensive.
Last edited on
It seems like new calling the default constructor is a real hassle. Is there a reason new doesn't just set aside space for n elements, and have me add elements as I choose (for arrays of course)?

And why the cast there?
Last edited on
Is there a reason new doesn't just set aside space for n elements, and have me add elements as I choose

That's what std::allocator does.
Well ill be damned. For some reason whenever I see that I thought it was some black magic. Time to go get that.

Is it different than placement new?
std::allocator<T>::construct() is a wrapper over placement new.
closed account (o1vk4iN6)
And why the cast there?


operator new() is essentially malloc and returns a void*.

Also i'd read into eastl and some limitations of the std::allocator for some purposes if you plan to implement your own.

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2271.html
Topic archived. No new replies allowed.