That's called a "garbage collector". There's a couple out there, but they don't really work too well. C and C++ weren't designed with garbage collection in mind.
When you want to make sure memory is being properly managed, you typically run the program through a memory debugger, like Valgrind or Electric Fence.
The heap doesn't have to be anywhere in particular, but it's definitely not part of the stack. The stack is allocated by the operating system and of fixed size.
I don't understand what you mean by "to have the program scope".
What Athar said is true, but the compiler is inherently incapable of performing that check because dynamic memory allocation occurs at run time.
I don't see where you're going with that, though.
@helops,
I am just trying to understand where that so called "heap" would be located. It wouldnt be in the processes's address space as such a thing would be lead to any kind of memory leak.
when you create two new objects (like in this case ObjName1, ObjName2) are two different pointers of type ClassName. Memory (typically 4 bytes) for these two pointers is allocated in process address space.
"new" creates memory for each of these objects in heap, which is of size of ClassName and points the two pointers respectively. The operator "new" ensures in allocating different memory for each of them.
All this memory creation is done in Virtual address space. Depending on the kind of memory management used (ie., paging or segmentation) the Virtual address space is mapped on to RAM during execution state of the process.
@jmc and skyrulz..
Thanks.. So if thats the case as skyrulz says.. when the process is over, the virtual memory space is freed rite? so in that case why do people say undeleted pointers lead to memory leak.. In short.. what is memory leaking??
Memory allocated by a process doesn't have to be freed by the system, but you'll have a hard time finding an OS that implements virtual memory and doesn't clean up after processes.
What do you think will happen if this program runs for long enough?
1 2
while (1)
newint(0);
A memory leak is memory that the process has lost control over. It can no longer be used, but it also can no longer be freed. That memory will stay there at least until the process terminates. If the process runs for a couple of seconds and leaks a few kilobytes, this is probably not a problem. The problem is with processes that run for a long time and leak memory at a constant rate. Such a process is bound to crash.
Continuing on skyrulz's helpful post, AFAIK the heap space and stack space are allocated at the opposite ends of the virtual space of a process. Stack space grows downwards and heap space grows upwards (or was it the other way round?).
Registers are used for fast, low-level operations. They can only stored data that's 32-bits in size (atleast the general-purpose ones) on a 32-bit machine. So they can store 32 bit ints, memory addresses and the like.
Registers are implemented in the CPU. They are quite different to the "normal" memory and since intended for very fast processing they are constructed from flip-flops.
Memory is often cached by the CPU into memory that's faster, smaller, and closer to it than main memory, but that's not the same as a register and the process cannot be controlled by the programmer.