what is the C++ counter part of C's REALLOC function?

Hi

what does is the C++ counterpart of C's REALLOC function? If C++ does not have a counterpart for it, then how does one reallocate memory?

Thank you in advance?
hehe, got bored doing nothing while waiting for replies for this post, so I scavenged the net for answers to these questions.

And indeed I found a lot.

1.) NEW is not a C++ function but an operator.
2.) There is no C++ counterpart for C's REALLOC function

with info I found stated in 2, I asked myself again, "So, how does one reallocate a memory allocated previously using NEW?".

Again, I asked the mighty GOOGLE for the answers. GOOGLE didn't failed me, and gave me several answers but I will post only the first one (but not necessarily the best or least).

Cant explain the code better than the page where I got this code, so just visit this page using this URL for the explanations encase the comments in the following codes below is not enought :). So here's the code:

by the way, before the code, let me credit the page where I got the codes :). I got it from this page :)

http://www.fredosaurus.com/notes-cpp/newdelete/55dynexample.html

so finally the codes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int max = 10;           // no longer const
int* a = new int[max];  // allocated on heap
int n = 0;

//--- Read into the array
while (cin >> a[n]) {
    n++;
    if (n >= max) {
        max = max * 2;            // double the previous size
        int* temp = new int[max]; // create new bigger array.
        for (int i=0; i<n; i++) {
            temp[i] = a[i];       // copy values to new array.
        }
        delete [] a;              // free old array memory.
        a = temp;                 // now a points to new array.
    }         
}
//--- Write out the array etc. 


the code is not complete but I know that you know how to complete it :)

Just encase you dont, just drop a line here and I will be more than willing to help you with it :)

Go Cplusplus.com !!!!! :)
Nice catch. Just one note: if you double "max" at each resize, pretty soon you have way too much space that you don't need. Better to add a fixed amount each time.

Doubling, with max starting at 1:
1 2 4 8 16 32 64 128 256 512 1024 2048 ... (hey! its the binary sequence!)
What if I only needed 1054 ints? Space wasted = 994 ints.

Adding a fixed amount, say 10:
10 20 30 40 ... 1040 1050 1060
Sure, it has more reallocations, but space wasted = 6 ints.

You can reduce the number of reallocations by knowing something about the size of your input data. If I were allocating on average 1000 ints with the smallest approaching 100-150, I might be inclined to set my fixed amount to add every reallocation to 200 ints or somesuch...

Hope this helps.

Hi Duoas :)

Dont worry with that program, actually, Im not going to use that one. THat just serves as my test program in learning dynamic allocation of memory.

I already knew that there is that NEW operator but could find any "reallocation" operator that comes with it. So I opted to use realloc since with it I dont have to create my own reallocating function. But unfurtuantely I always have a problem with dynamically allocating memory for string class. So with this, I decided not to use realloc.

Since Im not going to use realloc, I need to find a way as to how to reallocate by just using the new operator. And with the all trusty GOOGLE, found a way :) hehehe

3 cheers for GOOGLE! (Hep2x Huray!)3x

By the way, y movies database is almost done :)
want to beta test it? hehehe its not that much, still working on with the algorithms :) will add the aesthetics later on :)

Just drop me a line here or here "jdstufu@yahoo.com" if you want to beta test it :)


Thank you once again :)
There is no C++ version of realloc(), but you can create your own Realloc() function, but it would be platform specific and would usually have strict rules about how to allocate/deallocate memory.
Here's a C++ Realloc() for Windows that I came up with:
http://cboard.cprogramming.com/showpost.php?p=710912&postcount=29
Topic archived. No new replies allowed.