Ruby Ruby - 1 year ago 114
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;

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download