Dynamic Memory Allocation
with malloc() and free()

Overview


Files to Download

Exercises

  1. Study/compile/run mallocplay.c.
  2. Study/compile/run play.c. Then uncomment each of the three commented lines in turn the scenarios described below. After uncommenting a line and seeing its effect, comment it out again before uncommenting the next one. IMPORTANT NOTE: Don't generalize about the behaviors you notice. On a different system, or with a different program on the same system, the behavior may differ.
  3. Study/compile/run printTwice.c. It should print 0 thru 4 twice. Figure out why it doesn't. Fix it by writing code that uses pointer notation (with the * operator, not the [] operator).
  4. Study/compile/run debugit.c. It should be able to run "forever" without crashing. However in its current version it crashes after several iterations of the loop (the exact number is system dependent). Find out why the memory allocation error occurs and fix it so that the program will run "forever" (in which case the user needs to type CTRL C to abort it).
    number of malloc()'s: 
    1
    2
    3
    4
    5
    6
    7
    memory allocation error: data
    

Note


Created by Jean Griffin
Modified by Diana Palsetia