![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
|
C++'s ReDim
I am trying to help someone translate a large app from VB.NET into C++. There are alot of ReDim statements in the code. I know what ReDim does in VB.NET but I was wondering if there was a way to do such a thing in C++. Alot of the time, it's mostly this:
ReDim SomeArray(0) ----or---- ReDim Preserve SomeArray(0) Any insights would be greatly appreciated!
__________________
Children in the dark cause accidents, and accidents in the dark cause children. http://www.ronincoders.org |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
If you use STL thangys, such as a vector, you have the resize and reserve methods available. If you use malloc for an array, you can realloc. I'm not sure about "new", other than it winds up calling malloc, itself.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Jul 2005
Location: Germany
Posts: 69
Rep Power: 4
![]() |
Hi,
For the first case you can use realloc. But be aware that using realloc on an object created with the new-operator will lead to undefined behavior. For the second case u can use realloc too, but remember that changing the size to size less than the original will eat oldsize-newsize bytes of data. Again don't mix op-new and malloc/realloc. Also remember to call the right cleanup routine, free() or op-delete. If you want such behavior think about using std::vector or std::deque which might have some advanteges depending on your particular situation.
__________________
-= C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do succeed, you will blow away your whole leg. =- Bjarne Stroustrup |
|
|
|
|
|
#4 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
Quote:
As you say, the methods to resize an array depend on what you're calling an array; std::vector has resize() and reserve() methods; if you've created the array dynamically with malloc, either use realloc(), or replicate the operation using malloc(), copying the old array to the newly allocated memory, free() up the old array, and then reassign the pointer to point at the new array. if you've created the array dynamically with operator new[], allocate a new array with operator new[], copy the old array to the newly allocated array, delete[] the old array, and reassign the pointer to point at the new array. With a standard array, for example; int x[100]; |
|
|
|
|
|
|
#5 |
|
Expert Programmer
Join Date: Dec 2004
Posts: 794
Rep Power: 4
![]() |
you shouldn't be doing this with arrays; use vectors.
__________________
Few people deserve to be compared to (Rush) Limbaugh, most of them were convicted at the Nuremburg trials. --WilliamSChips on Slashdot |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|