Whenever memory is allocated like this, it is reserved on "The Heap". This is an area of memory allocated to your program by the operating system. By using the allocation functions in C++, e.g.: new()
or malloc()
(there are others too), a contiguous block of this heap is reserved, and the address of it (a pointer) is returned to the calling code.
So in your function:
int *allocater()
{
int *x = new int(1);
return x;
}
A single integer-sized piece of memory is reserved (probably 4-8 bytes), and the address of this memory is returned. This address is just a number, when the number is interpreted as a location in memory, it's called a pointer - but it's still just a number.
So when your function returns, the memory is still allocated on the heap. If your program forgets that number, this memory is "leaked" - you program cannot de-allocate it with delete()
, delete[]()
or free()
because you don't have the number to tell the de-allocation function where to free.
In your code, because you store the return value from allocater()
, it's possible to de-allocate the block with delete
. So you code works fine, and the memory is de-allocated properly.