malloc() (n., from the Latin mal-, which means bad, and the Latin locus, which means place) 1. a function to return a bad place to store data; a routine characterized by slowing down a program and wasting space. "Half my goddamn students used ~ to store heads for their linked lists. Didn't they learn about the & operator in 15-213?"


malloc() is a good first order approximation of a general way to get memory, but before you call it, consider where else you might want to store your data. What's the actual scope of your data? For how long does it have to be alive? Consider the following:

void do_something_with_an_element(int a)
{
  list *l = malloc(sizeof(*l));
  l->value = a;
  list_enqueue(some_list, l);
  do_some_things_with_the_list();
  list_remove(some_list, l);
  free(l);
}

There might not be a whole lot of logic there, but boy is malloc() slow! And as it turns out, in that case, the memory didn't need to come from malloc(); it could just as well have been stacked.

When you use malloc(), you might not always have options. But wouldn't it be nice if everyone exercised them when they did?