I have some code that allocates memory for classes using malloc / realloc and then using them again What I am working from below is a quote from:
Zero NewSubInstances () // Every worker thread has been invited to develop the subdirectory array and / / Define each new sub-instance using a specific method Rmb in // by subclass {If (slavetotalspace & gt; = totalobj) {return; } // Remember the current large size G4inintlowSpace = SlawotSpace; // Increase its size by some value (purely arbitrary) slavetotalspace = totalobj + 512; // New space offset offset now = (t *) reel (offset, slototospace * size (t)); If (offset == 0) {G4Exception ("G4VUPLSplitter :: NewSubInstances ()", "OutofMemory", FatalException, "can not malloc space!"); Return; } // Newly created objects need to be started (G4 IT i = MulototSpace; I & lt; SloughtotSpace; I ++) {Offset [I] .INALIZ (); }} Void FreeSlave () // Invited all threads to release the suburban array. {If (Offset!) {Return; } Free (Offset); Offset = 0; }
I know that malloc will not call the class creator, but this is done by the initial function. My question is how can I deal with the way to free the memory, which will call the destroyer of the class (classes usually allocate memory and will need to remove it)?
Thank you!
How can I deal with the way to free the memory? Are dynamically allocated memory and will need to remove it)?
The answer to your question looks like this:
zero free slave () {if (offset!) {Return; } // Call Destructors clearly (G4 index index = 0; index and lieutenant; slootot space; ++ index) offset [index]. ~ T (); Free (Offset); Offset = 0; }
He said, do not use malloc / realloc / free in C ++ (no, even if no excuse goes here ). Although this will probably work (not sure on this), this code is ambiguous / incompatible dependency, brittle, difficult to understand, and it violates the least surprising principle.
Also, please do not call your pointers "offset" and do not insert the C-style in C ++ (this is a bad practice).
Comments
Post a Comment