Expression 1 –
int a;
int* ap = &a;
Ans : This is an int pointer which will stay on the stack.
Expression 2 –
int a;
int*ap = new a; **// syntax error**
Ans: new
will allocate memory in heap and this needs manual cleanup (using delete
), whereas in Expression 1 it will automatically cleanup when it goes out of scope.
solved Are Both of these expressions same in C++? [closed]