"realloc()" behavior with "new"

Started by
11 comments, last by Bullmax 21 years, 2 months ago
I've read Thinking in C++ about "new" placement. Interesting, but I don't like the fact that I must destroy my objects explicitly by calling the destructor myself...

I thinked about it and realized that the technique of daerid would never cause mempory overlapping because new give you a completely free chunk of memory. If not, it would be insane... [as SabreMan said]

So I think that I would rather use is technique that consist of creating a new chunk representing the "reallocation", copying the old chunk content into the new chunk content and then destroy the old chunk.

There is a drawback...(as always). If I have a huge memory chunk (I don't have a representative example) to resize, I would need a ton of free memory to create the new chunk and keeping in mind that I will have to copy the whole old chunk into the new chunk... argh!

I don't think that I will encounter this catastrophic scenario, but... I don't like systematic scenario rejection when it is possible.

Am I taking a good decision ?


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

[edited by - Bullmax on January 30, 2003 1:30:13 AM]
/* Bullmax */-------------Reality has many forms : good and evil, black and white, yin and yang.
Advertisement
quote:Original post by Bullmax
I''ve read Thinking in C++ about "new" placement. Interesting, but I don''t like the fact that I must destroy my objects explicitly by calling the destructor myself...


You don''t have a choice. The object might have to inform other objects that it has moved, or it might internally hold pointers

quote:
So I think that I would rather use is technique that consist of creating a new chunk representing the "reallocation", copying the old chunk content into the new chunk content and then destroy the old chunk.


If you use your class'' copy constructor, you''ll be fine, otherwise you''ll get in big trouble very soon.

quote:
There is a drawback...(as always). If I have a huge memory chunk (I don''t have a representative example) to resize, I would need a ton of free memory to create the new chunk and keeping in mind that I will have to copy the whole old chunk into the new chunk... argh!


realloc suffered from exactly the same problem.

quote:
I don''t think that I will encounter this catastrophic scenario, but... I don''t like systematic scenario rejection when it is possible.


You will, one day, run out of memory. It is a certainty. Be prepared.

quote:
Am I taking a good decision ?


No. Learn to use standard containers. Only when they don''t fit the bill should you start mucking with manual memory management.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
quote:Original post by Bullmax
Am I taking a good decision ?

You''re making life unccecessarily hard for yourself. Forget about how you would do it with C, you''re programming in C++ now. Get a hold of Accelerated C++, and work through it.

This topic is closed to new replies.

Advertisement