Code code with appropriate tags, please. Absolutely unreadable happened.
- Actually, yes.
- We will write, and we will write. The program or throw exception, or will limp further.
- Lose 20 * sizeof (int) bytes.
Exactly in order to avoid such problems come up with smart pointers. They combine the best of stack and dynamic memory allocation. Stack memory is automatically cleaned when it goes out of scope, and the speaker allows you to allocate large amounts of memory, because stack is always not enough. Thus, creating a smart pointer on the stack, then we can not worry about whether to clean the memory. As soon as the pointer crashes, all memory indicated by it will be released. True, you need to be careful with the assignments and copying pointers. Because at the same time, several pointers will point to one memory area. And then all sorts of unpleasant things can happen. Therefore, further differentiation of smart pointers occurs. There are “strong” who own objects, and there are “weak” who do not participate in the counting of the number of links.
PS:
int * a = new [10];
so to write is incorrect. Probably. Because new takes type. Those. we need to rewrite it as:
int * a = new int [10]; // allocate memory for an array of 10 ints
PPS:
int * a = new [10];
int * b = new [100];
delete [] a;
a = b;
Yeah. And then you almost certainly do
delete [] a; delete [] b; //FAIL!?