Call delete at variable address

Why can I do:

int i = *(new int (5));

and successfully use iafter it,

but when I try:

delete &i;

I get a runtime error:

Unhandled exception in 0x5ddccaf7 (msvcr100d.dll) in Test.exe: 0xC00000FD: stack overflow.

If there iwas a link:

int & i = *(new int (5));

all of this (including delete) works great.

I know that it is not good to keep a dedicated memory handler in anything but a pointer, and *(new ...)horrible, but I just wonder why it newworks well, but deletefails.

// Below are just my guesses about the reason for this behavior:

, , (, , "", - , ), delete, , ​​ &i ( - ) ? ( , , )

+4
3

i . int 5, i, . , (new 'ed), .

, i new '. , &i . &i , , - .

+8

, i , new. new, . i, , delete undefined.

, , : new i " int". , , operator new, , , .

+4

:

int i = *(new int (5));

:

int* p = new int(5);
int i = *p;

i ( delete) .

In mathematical notation is &x == &yimplied x == y, but x == ydoes not mean &x == &y.

+4
source

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


All Articles