Ruby Ruby - 3 months ago 19
C++ Question

Memory allocation using pointer VS without using pointer

Is there any difference in terms of memory usage between the following two codes in C++?

int *val = new int(5);


and

int val = 5;

Answer

Yes, your first example requires at least sizeof(int*) (in automatic storage) + sizeof(int) (in dynamic storage). There may also be overhead from the memory management.

Your second example only requires sizeof(int) which may end up being omitted entirely (by optimization) depending on how its used.