Skip to content

Instantly share code, notes, and snippets.

@heftig
Forked from Gikoskos/test.c
Last active August 29, 2015 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heftig/30eef9e442a85c57c80d to your computer and use it in GitHub Desktop.
Save heftig/30eef9e442a85c57c80d to your computer and use it in GitHub Desktop.
memory problem in C
#include <unistd.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
char *foo2 ()
{
return "FOO";
}
char *foo1 ()
{
char *str = strdup(foo2());
str[2] = 'X';
return str;
}
int main (int argc, char *argv[])
{
char *str;
while (1) {
str = foo1();
puts(str);
free(str);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment