Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 10th, 2005, 4:00 PM   #1
Intimidat0r
Hobbyist Programmer
 
Intimidat0r's Avatar
 
Join Date: May 2005
Location: Don't know, but the padded walls are a nice touch.
Posts: 126
Rep Power: 0 Intimidat0r is an unknown quantity at this point
Send a message via ICQ to Intimidat0r Send a message via AIM to Intimidat0r Send a message via MSN to Intimidat0r Send a message via Yahoo to Intimidat0r
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
Intimidat0r is offline   Reply With Quote
Old Aug 10th, 2005, 6:29 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Aug 11th, 2005, 3:31 AM   #3
prolog
Programmer
 
Join Date: Jul 2005
Location: Germany
Posts: 69
Rep Power: 4 prolog is on a distinguished road
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
prolog is offline   Reply With Quote
Old Aug 11th, 2005, 4:43 AM   #4
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
Quote:
Originally Posted by DaWei
If you use malloc for an array, you can realloc. I'm not sure about "new", other than it winds up calling malloc, itself.
While it's true quite a few implementations (i.e. compilers and libraries) do it, there is no requirement for operator new to call malloc().

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];
there is no way in C++ of resizing it.
grumpy is offline   Reply With Quote
Old Aug 11th, 2005, 8:21 AM   #5
uman
Expert Programmer
 
Join Date: Dec 2004
Posts: 794
Rep Power: 4 uman is on a distinguished road
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
uman is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:23 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC