• Welcome to Valhalla Legends Archive.
 

ReDim?

Started by Eli_1, July 29, 2004, 12:27 AM

Previous topic - Next topic

Eli_1

This may sound like a dumb question, but does c/c++ have a ReDim equivalent?

Eibro

Perhaps, see realloc(). ReDim is simply a keyword which does some work under the hood. "create/copy/delete"
You need to create a new array of the desired size, copy all your data in, and remove the old array.

You could make a ReDim-esque function.

template < typename T > ReDim( T*& p, size_t oldsize, size_t newsize ) {

    T* newp = new T[ newsize ];
    memcpy( newp, p, oldsize );

    delete[] p;
    p = newp;
}

// ...

char* lpStr = new char[256];
ReDim< char >( lpStr, 256, 512 );
Code is untested, probably contains bugs. But I hope you get the idea.
Eibro of Yeti Lovers.

Eli_1

Yes I do. Thanks.  :)

K

Or, if you're using a vector, you can use the resize() function.