Memory management in C

This is a discussion on Memory management in C within the C Programming forums, part of the General Programming Boards category; Hey everybody. Just found this forum and even before posting this topic, Ive already answered some questions I had! Anyway, ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    6

    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?

    Thanks!

    Greetings from Paraguay, South America

  2. #2
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    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://irc.efnet.net
    channel: #c

  3. #3
    Jez
    Jez is offline
    The C-er
    Join Date
    Mar 2004
    Posts
    192
    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

    [code]

    int three()
    {
    int x;
    x=3;
    return x;
    }

    [\code]
    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, 11:27 AM
  3. Valgrind says I suck at memory management :)
    By carrotcake1029 in forum C Programming
    Replies: 6
    Last Post: 02-01-2009, 07:10 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Managing shared memory lookups
    By clancyPC in forum Linux Programming
    Replies: 0
    Last Post: 10-08-2003, 04:44 AM

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