Topic: NEW, DELETE, realloc() ?!
Author: NOSPAMsl@psycode.com (Gili)
Date: 1998/03/08 Raw View
Hello,
I use NEW and DELETE in my C++ programs a lot. However, I am still
unsure how realloc() is implemented in C++.. Is it safe to use NEW,
DELETE and realloc() with each other? Or is this as bad as free()ing a
NEWed variable? If this is the case, what's is C++'s equivilent for
realloc()? Thank you,
Gili
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "greg" <dont@spam.me>
Date: 1998/03/09 Raw View
Gili <NOSPAMsl@psycode.com> wrote in article
<bWLoegW7sFse-pn2-dFfNOD9dnD2W@portB08.Generation.NET>...
> Hello,
>
> I use NEW and DELETE in my C++ programs a lot. However, I am still
> unsure how realloc() is implemented in C++.. Is it safe to use NEW,
> DELETE and realloc() with each other? Or is this as bad as free()ing a
> NEWed variable? If this is the case, what's is C++'s equivilent for
> realloc()? Thank you,
Mixing the C storage allocation functions with the C++ storage
allocation operators is undefined behavior. There is no C++
equivalent to realloc, but sometimes a vector or deque is a
reasonable substitute for reallocating an array of objects.
Greg Colvin
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Oleg Zabluda <zabluda@math.psu.edu>
Date: 1998/03/09 Raw View
Gili <NOSPAMsl@psycode.com> wrote:
: Hello,
: I use NEW and DELETE in my C++ programs a lot. However, I am still
: unsure how realloc() is implemented in C++..
That's because there is no build-in equivalent for realloc() in C++,
other then what C provides you with.
: Is it safe to use NEW,
: DELETE and realloc() with each other?
No. In fact, it is guranteed to fail in all non-trivial cases.
You can use either new/delete, new[]/delete[] or
malloc/free/realloc/calloc together, but you can't mix them.
: Or is this as bad as free()ing a
: NEWed variable?
Yes.
: If this is the case, what's is C++'s equivilent for
: realloc()? Thank you,
There us none. All the arguments why there shouldn't be one, didn't
convince me at all. Unfortunatelym to write one yourself, you'd have
to rewrite all low-level memory menagement routines, including
C's realloc(). Fortunately, you can always hack one of freely
available ones, gnu malloc() being the best candidate.
However, the fact that you use new and delete in your programs a lot,
probably means that you don't use standard containers enough.
Maybe this is the real solution for you, instead of realloc()?
Oleg.
--
Life is a sexually transmitted, 100% lethal disease.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Edward Diener <eddielee@abraxis.com>
Date: 1998/03/09 Raw View
No, it is not safe to mix new, delete, and realloc. New and delete can be used
together or malloc, realloc, and free can be used together, but do not mix
these idioms. Realloc is done in C++ by allocating the new space you need via
new and then copying your objects to that new area.
Gili wrote:
> Hello,
>
> I use NEW and DELETE in my C++ programs a lot. However, I am still
> unsure how realloc() is implemented in C++.. Is it safe to use NEW,
> DELETE and realloc() with each other? Or is this as bad as free()ing a
> NEWed variable? If this is the case, what's is C++'s equivilent for
> realloc()? Thank you,
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]