Free memory.

This is a discussion on Free memory. within the C++ Programming forums, part of the General Programming Boards category; I just started out on the subject of freeing memory in the free store in my book and I was ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    62

    Free memory.

    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?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    18
    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.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    1,203
    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.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    18
    Quote Originally Posted by Shakti View Post
    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.
    if you look at the memory allocation of process,
    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.
    Last edited by hr1212s; 05-27-2010 at 10:42 PM.

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    1,203
    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).

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Wheee!

    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.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 04:04 AM
  2. How to read a txt file into an array
    By Hitsugaya_KK in forum C Programming
    Replies: 49
    Last Post: 08-22-2009, 02:22 PM
  3. Problems with shared memory shmdt() shmctl()
    By Jcarroll in forum C Programming
    Replies: 1
    Last Post: 03-17-2009, 10:48 PM
  4. Memory leak with detached pthreads - how to free?
    By rfk in forum Linux Programming
    Replies: 2
    Last Post: 08-17-2007, 06:50 AM
  5. free memory in structure
    By franziss in forum C++ Programming
    Replies: 22
    Last Post: 01-08-2007, 04:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21