|
Ew, no! next_item is a pointer. Setting it to NULL will leave the memory allocated, sure, but you'll have no record of the address, meaning the structure exists but your program has no way of getting back at it. If you try to indirect through a null pointer, you'll get a Segmentation Fault (on Unix; some equivalent, probably more serious, and more vague error on Windows toys).
In other words, if you were to omit delete, you'd still need to allocate again next time with new, but you'd leak memory each time this happened. If you want to keep a pool of memory on the heap so you don't have to keep allocating and deallocating, you have to work harder than this and keep a list of references to your available allocated memory that you can look up - effectively implement your own mini memory manager on top of the existing one.
|