-
-
Save heftig/30eef9e442a85c57c80d to your computer and use it in GitHub Desktop.
memory problem in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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