Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't the runtime environment decide to apply delete or delete[] instead of the programmer?

I've read that the delete[] operator is needed because the runtime environment does not keep information about if the allocated block is an array of objects that require destructor calls or not, but it does actually keep information about where in memory is the allocated block stored, and also, of course, the size of the block.
It would take just one more bit of meta data to remember if destructors need to be called on delete or not, so why not just do that?

I'm pretty sure there's a good explanation, I'm not questioning it, I just wish to know it.

like image 280
Petruza Avatar asked Dec 06 '11 18:12

Petruza


People also ask

What happens if you use delete [] instead of delete?

delete is used for one single pointer and delete[] is used for deleting an array through a pointer. This might help you to understand better.

Can I drop the [] when deleting array of some built in type char int etc )?

[16.13] Can I drop the [] when deleteing array of some built-in type (char, int, etc)? No! Sometimes programmers think that the [] in the delete[] p only exists so the compiler will call the appropriate destructors for all elements in the array.

Why do we need to use the delete or delete [] syntax to release memory after using the new keyword to allocate memory consider what happens if you don t?

Any time you allocate an array of objects via new (usually with the [ n ] in the new expression), you must use [] in the delete statement. This syntax is necessary because there is no syntactic difference between a pointer to a thing and a pointer to an array of things (something we inherited from C).

What does delete [] do in C++?

Delete is an operator that is used to destroy array and non-array(pointer) objects which are created by new expression. New operator is used for dynamic memory allocation which puts variables on heap memory. Which means Delete operator deallocates memory from heap.


2 Answers

I think the reason is that C++ doesn't force you into anything you don't want. It would add extra metadata and if someone didn't use it, that extra overhead would be forced upon them, in contrast to the design goals of the C++ language.

When you want the capability you described, C++ does provide a way. It's called std::vector and you should nearly always prefer it, another sort of container, or a smart pointer over raw newand delete.

like image 52
Mark B Avatar answered Nov 15 '22 19:11

Mark B


C++ lets you be efficient as possible so if they did have to track the number of elements in a block that would just be an extra 4 bytes used per block.

This could be useful to a lot of people, but it also prevents total efficiency for people that don't mind putting [].

It's similar to the difference between c++ and Java. Java can be much faster to program because you never have to worry about garbage collection, but C++, if programmed correctly, can be more efficient and use less memory because it doesn't have to store any of those variables and you can decide when to delete memory blocks.

like image 33
Austin Heerwagen Avatar answered Nov 15 '22 18:11

Austin Heerwagen