I just started out on the subject of freeing memory in the free store in my book and I was wondering how that is actually used most often.
All I can think of is that it is used as a replacement for variables so you can delete them once used to keep the memory usage of your program lower.
Am I right on this or is it more used for more advanced features that I will probably learn later on?
might be you are talking about memory allocation in heap.
in many application you can't know how much memory is required in your program, for example library application.
in this you cant know maximum memory requirement at time of programing, so instead of allocating larch chunk at compile time you use heap memory.
also if you allocate larch chunk(globally) at compile time your application size is increased.
Most OSes allocates a certain (small, relative to the total amount available in todays computers) amount of memory that can be used as stack for a program. If you need more memory than that you must use the heap in order to get more memory. hr12 is also correct, for instance if you want to build a linked list there is no way of knowing how many entries you will have. Thus you use the heap to allocate memory at run-time.
if you look at the memory allocation of process,
Originally Posted by Shakti
there are 2 areas 1)permanent storage area where program code and global variables are stored 2)this area is shared by stack and dynamic memory allocation(heap).
so 2nd part of memory is shared by stack and heap ,i.e when we create more variables on stack, heap area gets shrink. (imagine stack address gets decremented when we create local variable.)
you can't get extra memory by using heap, this is the same memory you can use for stack(local variable).
please comment on this.
The stack is allocated on a per process basis. There is only a limited amount of stack space a single process can used and if that limit is exceeded you will get stack overflow. Thats for instance why you cant allocate say a 200MB array on the stack...even if you have that much RAM free you will most likely get a stack overflow anyway. However you should have no problems allocating 200MB on the heap (providing you have the memory available).
Memory for a process can be classified into three parts:
1) Static memory. This is the program code and global lifetime variables. It's allocated at process start and lives until the process dies. It may be partially dynamic due to loading an unloading of dynamic libraries.
2) Stack space. Every thread of the program has at least one stack. But stack space is usually tightly limited by the OS. For example, by default a stack in Win32 may not grow beyond 1 MB. The stack memory lifetime is on a strict LIFO basis. As you enter functions, the stack grows, as you leave them it shrinks.
3) Heap space. All the memory that's left after the first two areas have taken their pick. Managed in some scheme so that you can allocate and free blocks in any order. You therefore use the heap for these purposes:
a) Allocating memory blocks that you can't allocate statically (because you don't have a fixed number of them) and that are too large for the stack.
b) Allocating memory blocks that need to live longer than the allocating function (which stack space can't do) and the number of blocks is not statically known.