COMP1927 - Malloc
If passing an array to a function from the code section, we cannot edit it. Instead we must make a copy (malloc it) and then return the address.
If we just create an array in a function (without malloc) you return a pointer to where it was but stacks get destroyed when functions exit (and hence your array disappears).
The only acceptable way to create arbitrary length things is to malloc them – variable length arrays are not cool.
Malloc() always needs to be free()’d
Current = (Block *) malloc (sizeof(Block))
Do not do the “(Block *)”, typecasting malloc’s return can hide type clashes
page revision: 1, last edited: 26 Jul 2011 13:23