 <p>It seems like <code>buffer</code> is probably set to <code>NULL</code> or some other invalid pointer and probably segfaults when you dereference it. It could also be your first call to <code>free</code> if the pointer is invalid. Ideally you need to show us the code that calls this function.</p> <p>Also keep in mind it's bad form to call a matching <code>malloc</code> and <code>free</code> in different functions. Unless that function has only one purpose, to allocate a new structure or to free an existing one (In other words allocation of any resource should be done in the same function as deallocation of that same resource. The only exception is a function that composes more complex allocations and deallocations).</p> <pre><code>int load_filenew(char *filename, char **buffer) { int size = 0; FILE *fp = 0; if(buffer == NULL) { return 1; } fp = fopen(filename, "rb"); if (!fp) { printf(" fopen failed.\n"); return 2; } fseek(fp, 0, SEEK_END); size = ftell(fp); fseek(fp, 0, SEEK_SET); if (size) { *buffer = (char *)malloc(size + 1); if (!*buffer) { printf(" malloc failed.\n"); fclose(fp); return 3; } memset(*buffer, 0, size + 1); fread(*buffer, size, 1, fp); (*buffer)[size] = '\0'; } else { fclose(fp); return 3; } fclose(fp); return 0; } </code></pre>