Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Bullmax

"realloc()" behavior with "new"

This topic is 5650 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.)

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 X

TMP 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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!