"realloc()" behavior with "new"

Started by
11 comments, last by Bullmax 21 years, 2 months ago
Hello camarades, I have a basic C to C++ question. By using the "malloc()", "calloc()", "realloc()" family, I can allocate memory. With "realloc()", I can pass a pointer that points to a previously allocated memory block and I would be able to resize the memory block by allocating only the offset required (I think so). The problem is that it does not call the constructor if I allocate memory for a class instance. I know that "new" will call the constructor if the class instance has one, but I don''t know if "new" will have the same behavior as "realloc()". Does someone knows if it the same of if I can simulate this. Maybe with the "new" operator overloading or .... I don''t know. /* Bullmax */ ------------- Reality has many forms : good and evil, black and white, yin and yang.
/* Bullmax */-------------Reality has many forms : good and evil, black and white, yin and yang.
Advertisement
I''m pretty sure realloc just wraps the behavior of deleting the memory, allocating a new block, and copying the old contents into the new buffer - nothing special. If you have memory you want to reallocate, new the new memory, copy the contents of the buffer up to the shorter of the two buffers, and delete the old block. Make a function out of it, but don''t go overloading the new operator.
quote:Original post by Zipster
I''m pretty sure realloc just wraps the behavior of deleting the memory, allocating a new block, and copying the old contents into the new buffer - nothing special.

Not necessarily - that''s the beauty of it. It might do this, but it might also simply allocate more memory at the end of the existing block, which is much more efficient; if there isn''t sufficient memory there, it will attempt to allocate an entirely new block and copy the data there. (... All according to the MSDN docs.)
That is true, but you have to ask yourself what practical use it really has. It might keep the same pointer, it might not. You can't tell before you call it.

So what you do is assign the return value to the same pointer.
Sure, it saves you a few lines of code, but since you never know what's going to happen when you call it, in effect, there's no advantage to using realloc() over a call to new[] and then an assignment.

The other option, if you're dead set on using realloc() is to use placement new on each element of your realloc()'ed block.

But then, that's even MORE lines of code. Personally, I'd just suggest doing this:
Object* tmp = new Object[count];for(int i = 0; i < count; ++i)  tmp[i] = old[i];delete [] old;old = tmp;  


But then again , if you're gonna go to that much trouble, just use a std::vector, and all your problems are solved.

[edited by - daerid on January 28, 2003 12:19:29 AM]
daerid@gmail.com
quote:Original post by Bullmax
The problem is that it does not call the constructor if I allocate memory for a class instance. I know that "new" will call the constructor if the class instance has one, but I don''t know if "new" will have the same behavior as "realloc()".

An object''s size is fixed at compile-time, so it is meaningless to perform a runtime realloc.
SabreMan :
I wanted to create object instances into memory, not resize the size of my class.

daerid:
Is there a chance that your code could erase my old memory. I mean if the tmp block would overlapp the old block, you could lose data, no ?

     0x4500 | 0x4600 | 0x4700 | 0x4800 | 0x4900 | 0x5000     ---------------------------------------------------OLD     X        X        X        XTMP                       X        X        X       X  


Could this happen in memory ? If yes, it's dangerous....

*I mean the copy operation from "old" to "tmp" could make lose data if it's overlapping. Try the algorithm on this "graphic"

[edited by - Bullmax on January 29, 2003 7:54:05 AM]
/* Bullmax */-------------Reality has many forms : good and evil, black and white, yin and yang.
quote:Original post by Bullmax
SabreMan :
I wanted to create object instances into memory, not resize the size of my class.

I see. Look up "placement new" in your favourite C++ reference.
Look up placement new. It's probably what you are looking for.

[Edit: beaten to it!]


[edited by - MadKeithV on January 29, 2003 7:58:05 AM]
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
quote:Original post by Bullmax
daerid:
Is there a chance that your code could erase my old memory. I mean if the tmp block would overlapp the old block, you could lose data, no ?

That''s not possible, and would be quite insane. You really should learn about the standard containers, such as std::vector, which do all manner of stuff behind the scenes such as you are describing. For instance, vector has a reserve() member, which pre-allocates storage, and then uses placement new to perform in-place construction of items added to the vector. Chances are vector (or some other standard container) already does for your needs.
Thanks for all these answer !
I''ll give a try and there will be feedback. hang on


/* Bullmax */
-------------
Reality has many forms : good and evil, black and white, yin and yang.
/* Bullmax */-------------Reality has many forms : good and evil, black and white, yin and yang.

This topic is closed to new replies.

Advertisement