Re: Overloading new[]



Victor Bazarov <v.Abazarov@xxxxxxxxxxxx> wrote:
> MyClass* arrayCreator(size_t howmany, int argument) {
> char *storage = new char[howmany * sizeof(MyClass)];
> for (int i = 0; i < howmany; ++i)
> new (storage + sizeof(MyClass) * i) MyClass(argument);
> return reinterpret_cast<MyClass*>(storage);
> }
>
> void arrayDisposer(MyClass* arr)
> {
> delete[] reinterpret_cast<char*>(arr);
> }

It would be prudent to run destructors before freeing the memory. And
for that, you would need to keep track of the number of elements
allocated.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


.



Relevant Pages

  • Re: Overloading new[]
    ... It would be prudent to run destructors before freeing the memory. ...
    (microsoft.public.vc.language)
  • Re: *** glibc detected *** ./a.out: double free or corruption
    ... Sometimes i have problems when freeing the memory of certain ... NULL invokes undefined behavior. ... returned from malloc and again when it is received by removeFPage. ...
    (comp.lang.c)
  • Re: Destructor: not gauranteed to be called?
    ... > implementation detail for destructors in C++. ... Destruction is a language ... Resource automatic freeing (mainly memory), ... The problem is that most developper know about finalizers (which ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Garbage collection
    ... which is nothing to do with allocation or destructors. ... If a program is not invoking destructors properly, ... in my reasoning, it is better, yes, to free up objects as soon as possible (after all, this saves memory footprint and generally makes performance better). ... //done with buffer so I no longer give a crap ...
    (comp.std.c)
  • Re: [PATCH] [Request for inclusion] Filesystem in Userspace
    ... > I don't see how this makes more problems to userspace filesystems. ... > With FUSE the allocation is NOT limited. ... because you do not have reserved pools to use for freeing memory. ...
    (Linux-Kernel)