How is memory managed?

Pages: 12
When we say

1
2
ClassName *objName = new ClassName();
   delete objName;


its obvious that memory is allocated for the object and freed when delete is called.

So how does the compiler keep track of the amount of memory that has been allocated for the obj??
Last edited on
Last edited on
A typical implementation will store the length of the memory block in the few bytes
before the pointer that is returned by malloc.
Is there a memory manager kind of thing which keeps track of all the memory that is allocated???

And one more thing, where is the heap actually?? is it a part of the stack that has the program scope??

thanks,
Karthick
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".
ok.. let me put my question this way.. when i say

ClassName *objName1 = new ClassName();


and then again

ClassName *objName2 = new ClassName();

Now does the compiler make sure that the memory allocated for the objName2 is different from the objName1??
Maybe. I see no reason why it would have to though.

Er, doh. I misread the question. >_> Yes it does.
Last edited on
Yes, two different objects are guaranteed to have different addresses. Even if the object size would be zero.
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.

Will it be part of RAM? or somewhere else?
Paging and segmentation is a quite difficult job, which is fortunately usually done by your system.
If you want to take a little insight into memory management:
VM in Windows NT - http://msdn.microsoft.com/en-us/library/ms810616.aspx
Memory management in MSW - http://msdn.microsoft.com/en-us/library/aa366779%28VS.85%29.aspx
Documentation for Linux memory management: http://linux-mm.org/
Visualisation of VM for Linux: http://freshmeat.net/projects/vmsymulator
Easy explanation of yome parts of VM - http://chortle.ccsu.edu/AssemblyTutorial/Chapter-33/ass33_1.html
Access system reserved VM - http://www.codeproject.com/kb/system/soviet_kernel_hack.aspx
Last edited on
hi all,

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.
Last edited on
@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)
    new int(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.
Guys.. one more doubt relating to memory management..

Where is static memory?? can static memory be assumed as shared heap memory?

Question 2
========

Can memory be allocated in register??
Last edited on
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?).

Static memory is allocated in the "data area" of the virtual address space of a process, see fig 5 in
http://www.ualberta.ca/CNS/RESEARCH/LinuxClusters/mem.html
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.
Excellent link DexterMorgan.. Thanks.. I have read somewhere that register int i=0 .
Pages: 12