Valhalla Legends Archive

Programming => General Programming => C/C++ Programming => Topic started by: Eli_1 on July 29, 2004, 12:27 AM

Title: ReDim?
Post by: Eli_1 on July 29, 2004, 12:27 AM
This may sound like a dumb question, but does c/c++ have a ReDim equivalent?
Title: Re:ReDim?
Post by: Eibro on July 29, 2004, 12:50 AM
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.
Title: Re:ReDim?
Post by: Eli_1 on July 29, 2004, 11:36 AM
Yes I do. Thanks.  :)
Title: Re:ReDim?
Post by: K on July 29, 2004, 03:27 PM
Or, if you're using a vector, you can use the resize() function.