Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Memory reallocation

Status
Not open for further replies.

fabien

Technical User
Sep 25, 2001
299
AU
Hi!

Say that I have a simple array like float* r = new float[10]; Then I do some operations etc on it. Later on I realise that 10 is not enough but I need 100 values. How do I reallocation memory to r without loosing its data?

Many thanks!
 
Like this:
Code:
float* r = new float[10];
size_t size=10;
...
float *temp = new float[100];
for(int i = 0; i < size; ++size)
{
   temp[i] = r[i];
}
delete r;
r = temp;
temp = NULL;
size = 100;
 
malloc() and free() Versus new and delete
Last updated Apr 21, 2004.

C++ still supports the C library functions malloc() and free(). This backward compatibility with C is useful in three cases:

Combining original C legacy code with C++ programs

Writing C++ code that's meant to be supported in C environments

Implementing or overriding new and delete by calling malloc() and free()

Otherwise, malloc() and free() are not to be used in C++ code because they don't support object semantics. Furthermore, the results of calling free() to release an object that was allocated by new, or of using delete to release memory that was allocated by malloc(), are undefined. The C++ standard doesn't guarantee that the underlying implementation of operator new uses malloc(); in fact, on some implementations malloc() and new use different heaps.
from:
new and delete are prefered in C++, and realloc on something declared with new is undefined (as realloc calls free)
 
We do it all the time, all depends on the compiler.

#include "stdlib.h"
#include "stdio.h"
#include "memory.h"

void main()
{
int a;
float* r = new float[10];

for (a=0;a<10;a++)
r[a] = a;

for (a=0;a<10;a++)
printf("\n%d %f",a,r[a]);

float*t = (float *) realloc( r,100);
if (!t)
{
printf("\n error");
}

for (a=10;a<100;a++)
t[a] = a;


for (a=0;a<100;a++)
printf("\n%d %f",a,t[a]);

}
 
The term "all depends on the compiler" worries me...
If it doesn't work on all C++ standard compliant compilers, it's bad code.
 
Tried it on watcom, borland, gnu, and SCO compilers, and get same result with each.

 
Why use code that is not guaranteed to work, and that you know will fail in some situations, when you have a perfectly valid alternative?
 
Well you certainly don't want to mix malloc/free with new/delete. Only new/delete guarantee that your class constructors and destructors will be called.

Code:
#include <iostream>
using std::cout;
using std::endl;

int ctor_count = 0;
int dtor_count = 0;

// simple class, just to count things happening
class moo {
    public:
    moo(void);
    ~moo(void);
};

moo::moo ( void ) {
    ctor_count++;
}
moo::~moo ( void ) {
    dtor_count++;
}

int main ( ) {
    ctor_count = dtor_count = 0;
    moo *bar = new moo[5];
    cout << "Using New   : Constructed = " << ctor_count
         << " Destructed = " << dtor_count << endl;
    delete [] bar;
    cout << "Using Delete: Constructed = " << ctor_count
         << " Destructed = " << dtor_count << endl;

    ctor_count = dtor_count = 0;
    // casting is necessary for the return result of malloc
    // and constructors / destructors are NOT called
    moo *foo = (moo*)malloc( 5 * sizeof *foo );
    cout << "Using Malloc: Constructed = " << ctor_count
         << " Destructed = " << dtor_count << endl;
    free( foo );
    cout << "Using free  : Constructed = " << ctor_count
         << " Destructed = " << dtor_count << endl;
}

With this, I get
[tt]Using New : Constructed = 5 Destructed = 0
Using Delete: Constructed = 5 Destructed = 5
Using Malloc: Constructed = 0 Destructed = 0
Using free : Constructed = 0 Destructed = 0
[/tt]

> Later on I realise that 10 is not enough but I need 100 values.
How about
Code:
vector<float> r;
The STL provides a whole collection of goodies.
Vectors for example will resize themselves if you let them, but if its easy for you to spot, it can be more efficient to make the size much bigger in one hit rather than lots of smaller hits.

--
 
> We do it all the time

Who's "we" so I know not to buy anything from you?

realloc on memory you got from new? I suggest that you seriously reconsider that practice.

Unless you have some magical, mythical C and C++ library/compiler combo that specifically tells you that using realloc on new-acquired memory is ok, then you're playing with fire.

 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top