Why does C ++ still have the delete [] AND delete operator?

How difficult is it to implement array deletion using a single keyword? Is it less effective?

+5
source share
4 answers

First, let me summarize what both do: deletecalls the destructor for one object and frees up memory; delete[]calls the destructor of a certain number of objects and frees memory.

It would be possible to collapse them into the same operation: “calls the destructor of a certain number of objects and frees memory”. After all, this is a number.

Both deleteand delete[]are working with a pointer. Like this:

foo* ptr;

. , , delete. . . . - . , new[] .

++ " , ", : delete , new doesn ' t .

+16

, , delete , , , . , .

C... , , , ++ ( ), new T[N] new T, , delete .

+6

This is because one of the main principles of C ++ design is that you do not pay for what you do not use. new[]/ delete[]have an additional cost, and it was considered that their usefulness (I never used C ++ at that time in 25 years.) It was limited enough that it did not justify adding this value to non-array new/ delete.

+5
source

Features like this cannot simply be removed from a language event if it was easy to implement. Do you voluntarily come and correct all my code after such changes?

-3
source

All Articles