简体   繁体   中英

Using malloc/free with classes

I have some code that allocates memory for classes using malloc/realloc and then deletes them again using free. Below is an extract from what I'm working with:

    void NewSubInstances()
  // Invoked by each worker thread to grow the subinstance array and
  // initialize each new subinstance using a particular method defined
  // by the subclass.
{
    if (slavetotalspace  >= totalobj)  { return; }
    //Remember current large size
    G4int originaltotalspace = slavetotalspace;
    //Increase its size by some value (purely arbitrary)
    slavetotalspace = totalobj + 512;
    //Now re-allocate new space
    offset = (T *) realloc(offset, slavetotalspace * sizeof(T));
    if (offset == 0)
    {
        G4Exception("G4VUPLSplitter::NewSubInstances()",
                    "OutOfMemory", FatalException, "Cannot malloc space!");
        return;
    }
    //The newly created objects need to be initialized
    for (G4int i = originaltotalspace; i < slavetotalspace; i++)
    {
        offset[i].initialize();
    }
}

void FreeSlave()
  // Invoked by all threads to free the subinstance array.
{
  if (!offset)  { return; }
  free( offset);
  offset = 0;
}

I know that malloc will not call the constructor of the class, but this is dealt with by the initialize function. My question is: how can I deal with the freeing of the memory in a way that will also call the destructor of the classes (the classes generally have dynamically allocated memory and will need to delete it)?

Thanks!

how can I deal with the freeing of the memory in a way that will also call the destructor of the classes (the classes generally have dynamically allocated memory and will need to delete it)?

The answer to your question looks like this:

void FreeSlave()
{
    if (!offset)
    { return; }
    // call destructors explicitly
    for(G4int index = 0; index < slavetotalspace; ++index)
        offset[index].~T();
    free( offset);
    offset = 0;
}

That said, DO NOT use malloc/realloc/free in C++ (no, not even if any excuse goes here ). While it will probably work (not sure on this), this code is obscure/has non-obvious dependencies, is brittle, difficult to understand, and it violates the principle of least surprise.

Also, please do not call your pointer "offset" and do not use C-style casts in C++ (it's bad practice).

You may use :

void FreeSlave()
{
    if (!offset) { return; }
    for (G4int i = slavetotalspace; i != 0; --i) {
        offset[i - 1].~T();
    }
    free(offset);
    offset = 0;
}

我建议您使用destroy()方法...因为您已经具有initialize() ...调用析构函数的方式尽管允许,但确实很丑陋,感觉就像是被黑客入侵了。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM