Last active
August 29, 2015 14:28
-
-
Save Gikoskos/d4a32a0f12f686cc258a to your computer and use it in GitHub Desktop.
no more memory problem in C
This file contains hidden or 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 *bar = malloc(sizeof(char)*4); | |
strcpy(bar, foo2()); | |
return bar; | |
} | |
int main (int argc, char *argv[]) | |
{ | |
char *str; | |
while (1) { | |
str = malloc(sizeof(char)*4); | |
char *temp = foo1(); | |
strcpy(str, temp); | |
free(temp); | |
free(str); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment