What is the difference if I do
int *i = new int;
*i = 5;
*(i+1) = 20;
and
int *i2 = new int [2];
i2[0] = 5;
i2[1] = 20;
I can access and use these 2 pointers the same way but what is the difference between these 2 examples and what errors can occur if I don’t allocate enough memory, as in the first example?
The difference is the first one invokes undefined behaviour. Anything could happen, including a program crash, or data corruption, or even simply just “working”.