[Solved] Difference of variable initialization in C++ [duplicate]


“Is there any difference between these 2 ways of storing an integer?”

Yes, there is a significant difference.

 int X = 100;

Initializes a variable X on the stack with the value 100, while

int *pX = new int(100);

allocates memory for an int on the heap, kept in pointer pX, and initializes the value to 100.

For the latter you should notice, that it’s necessary to deallocate that heap memory, when it’s no longer needed:

 delete pX;

1

solved Difference of variable initialization in C++ [duplicate]