malloc questions

Discussion in 'Mac Programming' started by larswik, Jan 10, 2011.

  1. larswik macrumors 68000

    Sep 8, 2006
    So now in the book 'Learning C on the Mac' He is covering that I need to allocate a block of memory that I am responsible allocating, freeing and keeping track of. His DVD tracking program makes scene, why create a 5000 DVD struct for a few DVD's, allocate memory as you add new ones.

    My Questions:

    1. Everything in the book so far I have been able to do without allocating memory. The computer seems to allocate memory behind the scenes when the compiler creates my variables and temporary memory for my Functions.

    2. In programs you folks write, do you always allocate and free memory for all your projects, or just in books examples of the DVD library when you allocate memory for each DVD title instead of saying myDvdTitle[5000]

  2. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    The variables you declare are "automatic". Space is allocated in an area of memory called the stack to store these. When a function returns, its area of the stack is no longer used, and this space will be used for subsequent functions. However, there is not as much room available in this area of memory, so a huge array of structs may overfill it and your program will crash. The other section of memory where dynamically allocated items are placed is called the heap. static variables also live here so they can have one home during the whole run of your program.

    When you need a very large chunk of memory or room for N items where N will only be known at runtime, you must allocate memory. Often programmers will use a "big enough" automatic array that turns out to be too small resulting in overflows, causing crashes and possibly security problems. On the flipside, proper memory management with malloc/free is easy to mess up resulting in leaks (memory the program won't release to the OS) or crashes if you try to free the same block many times.

    This is one of the most important parts of dealing with C, and one of the easiest to mess up. Read the chapter a few times and ask a lot of questions.

  3. jiminaus macrumors 65816


    Dec 16, 2010
    You're now moving from static memory allocation (ie memory allocation that cannot change) to dynamic memory allocation (ie memory allocation that can change).

    Commercial-quality won't allocate a 5000 element array to hold DVDs because what happens if someone wants to store 5001 DVDs. With dynamic memory your program will support libraries of only a few DVDs without wasting memory while being able to grew to hold as many DVDs as will fit in the computer's memory.

    In programs I write, for something like holding the library of DVDs, I'd absolutely be allocating and freeing memory (1). Generally I would use a mix of static and dynamic memory allocation. On a case-by-case basis, I'd judge whether the hassle/risk of dynamic memory allocation out weights the limitation of static memory allocations. With experience, this becomes a instinct.

    (1) Not directly. I'd be using a higher-level container. There are none provided with C's standard library like there is with Objective-C and C++, though.
  4. larswik thread starter macrumors 68000

    Sep 8, 2006
    Thanks for the information. I have been working the last few days and tonight I will dig into the book again. With more questions I am sure.


Share This Page