freeing mallocs

Barry Margolin barmar at think.com
Wed Apr 17 13:31:34 AEST 1991


In article <=-4_MX. at cs.widener.edu> sven at cs.widener.edu (Sven Heinicke) writes:
]question 26 of the FAQ is "How do I dynamically allocate a
]multidimensional array?".  To deallocate a multidimensional array can
]I just do a:
]
]                free(array);
]
]Or do I have to do this:
]
]                for(i = 1;i < nrows; i++)
]                    free(array[i]);
]                free(array);

Good rule of thumb: you should call free() as many times as you called
malloc().  This includes calls to malloc() that are done for you by library
routines.

Free(x) can only free as much stuff as was allocated by the call to
malloc() that returned x.

--
Barry Margolin, Thinking Machines Corp.

barmar at think.com
{uunet,harvard}!think!barmar



More information about the Comp.lang.c mailing list