Thread: Memory management in C

  1. #1
    Registered User
    Join Date
    Mar 2004

    Memory management in C

    Hey everybody. Just found this forum and even before posting this topic, Ive already answered some questions I had!

    Anyway, anybody knows how memory is managed in the C language? For example, how variables, functions, structures, etc are stored, or why do global variables take more space in memory than local ones?


    Greetings from Paraguay, South America

  2. #2
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    I have never heard that global functions takes up more than local. The way variables are stored in C is different in every OS. Your varialbes are stored in RAM at certain memory addresses. A struct is a special variable but it is still stored their. If you use the register keyword(I belive) it will store it on your chip. Noone should ever recommend this keyword though knowing that your compiler is smarter than you in determing which variables to store in your register. I hope that helps if I have any errors someone feel free to correct me
    Help populate a c/c++ help irc channel
    server: irc://
    channel: #c

  3. #3
    The C-er
    Join Date
    Mar 2004
    Hi nicolas.

    Actually you're kind of right about globals using more space than local variables.

    Local variables are only allocated storage during their scope, i.e. while they are valid during a program. For instance


    int three()
    int x;
    return x;

    the variable x only uses memory while the function is being executed. A global variable is using space all the time the program is running. I guess if you had a large program, all using globals (a very bad idea) then a lot of space would be wasted.

    Run time storage in programs is normally held in one of two places, the stack, and the heap.

    Local variables are held in the stack. The stack is kind of like a stack of plates in a restaurant, the items are added to the top, and removed from the top. So the stack grows and shrinks according to what's on it. Function return addresses go on the stack also.

    Global and Static variables are in the heap.
    Malloc'd memory goes in the heap too.
    Functions (in fact the whole program code) also is in the heap.

    Heap is main area of memory allocated by the operating system. It's more flexible because it is randomly accessible, but the overheads of allocating and deallocating are higher, this is dealt with by the operating system.

    chrismiceli is right about the register storage. Using register keyword is a hint to the compiler to use the fastest possible storage for a variable. The registers exist within the CPU itself.

    Hope this answers your question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tools for finding memory leaks
    By stanlvw in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 11:41 AM
  2. Replies: 7
    Last Post: 02-06-2009, 12:27 PM
  3. Valgrind says I suck at memory management :)
    By carrotcake1029 in forum C Programming
    Replies: 6
    Last Post: 02-01-2009, 08:10 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Managing shared memory lookups
    By clancyPC in forum Linux Programming
    Replies: 0
    Last Post: 10-08-2003, 04:44 AM