Ruby Ruby - 8 months ago 69
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);


int val = 5;


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.