bool example1()
{
long a;
a = 0;
cout << a;
a = 1;
cout << a;
a = 2;
cout << a;
//and again...again until
a = 1000000;
cout << a+1;
return true;
}
bool example2()
{
long* a = new long;//sorry for the misstake
*a = 0;
cout << *a;
*a = 1;
cout << *a;
*a = 2;
cout << *a;
//and again...again until
*a = 1000000;
cout << *a + 1;
return true;
}
Note that I do not delete a in example2(), just a newbie’s questions:
1. When the two functions are executing, which one use more memories?
2. After the function return, which one make the whole program use more memories?
Thanks for your help!
UPATE: just repace long* a; with long* a = new long;
UPDATE 2: to avoid the case that we are not doing anything with a, I cout the value each time.
Original answer
It depends and there will be no difference, at the same time.
The first program is going to consume
sizeof(long)bytes on the stack, and the second is going to consumesizeof(long*). Typicallylong*will be at least as big as along, so you could say that the second program might use more memory (depends on the compiler and architecture).On the other hand, stack memory is allocated with OS memory page granularity (4KB would be a good estimate), so both programs are almost guaranteed to use the same number of memory pages for the stack. In this sense, from the viewpoint of someone observing the system, memory usage is going to be identical.
But it gets better: the compiler is free to decide (depending on settings) that you are not really doing anything with these local variables, so it might decide to simply not allocate any memory at all in both cases.
And finally you have to answer the “what does the pointer point to” question (as others have said, the way the program is currently written it will almost surely crash due to accessing invalid memory when it runs).
Assuming that it does not (let’s say the pointer is initialized to a valid memory address), would you count that memory as being “used”?
Update (
long* a = new longedit):Now we know that the pointer will be valid, and heap memory will be allocated for a
long(but not released!). Stack allocation is the same as before, but nowexample2will also use at leastsizeof(long)bytes on the heap as well (in all likelihood it will use even more, but you can’t tell how much because that depends on the heap allocator in use, which in turn depends on compiler settings etc).Now from the viewpoint of someone observing the system, it is still unlikely that the two programs will exhibit different memory footprints (because the heap allocator will most likely satisfy the request for the
new longinexample2from memory in a page that it has already received from the OS), but there will certainly be less free memory available in the address space of the process. So in this sense,example2would use more memory. How much more? Depends on the overhead of the allocation which is unknown as discussed previously.Finally, since
example2does not release the heap memory before it exits (i.e. there is a memory leak), it will continue using heap memory even after it returns whileexample1will not.