Segmentation fault after free(), what are the common causes for this?

Pieter picture Pieter · Feb 21, 2010 · Viewed 25.3k times · Source

I get a segmentation fault after freeing a certain pointer:

free(studentDB->name);

I can get its value without any errors or warnings:

printf("[DBG] studentDB->name: %s\n", studentDB->name);

However, as I said, the program crashes when I try to free it. What are the most common causes for a free command leading to a segmentation fault?

Answer

Carl Norum picture Carl Norum · Feb 21, 2010

If you didn't malloc() it, you can't free() it. Where does studentDB->name come from?