C ++ removes operator confusion

Possible duplicate:
(POD) free memory: delete [] is equal to deletion?

char* pChar = new char[10];

delete pChar; // this should not work but it has same effect as 
              // delete[], WHY?
              // I know this is illegal, but why does it work?
+3
source share
5 answers

It may have the same effect, but it is not. If your array type was an abstract data type (i.e. Class), then destructors from the last nine elements would not be called.

+12
source

Because you are lucky. This behavior is undefined. One possibility of undefined behavior is that nothing bad happens, even if something bad really happened. You may not find out later.

. ( ): https://isocpp.org/wiki/faq/freestore-mgmt#delete-array-built-ins

+8

. , . , undefined, "" , , , , " " .

+5

undefined. " " "undefined", , - . , . , ?

EDIT: , ...

+1

Microsoft Visual Studio . , , .

delete [] , , , - ( ).

, delete pointerToBaseClass , , , , .

If you make the mistake of delete [], it may also have problems with tools that replace the distributor (debuggers, delimiters of all kinds, etcetc) and custom distributors that your users can use.

0
source

Source: https://habr.com/ru/post/1752341/


All Articles