2

With:

char *x = malloc(1024);
strcpy(x, "asdf");
x = strdup(x);
free(x); // OK
free(x); // Segfault

If I just free it once, will I still be leaking? And if so, how to avoid it?

Matoe
  • 2,742
  • 6
  • 33
  • 52

1 Answers1

5

You leak memory because you forget the first pointer. Do it like this:

char * x = malloc(1024);
strcpy(x, "asdf");
char * y = strdup(x);

free(x); 
free(y);
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084