Skip to content

Instantly share code, notes, and snippets.

@Gikoskos
Last active August 29, 2015 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Gikoskos/d4a32a0f12f686cc258a to your computer and use it in GitHub Desktop.
Save Gikoskos/d4a32a0f12f686cc258a to your computer and use it in GitHub Desktop.
no more 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 *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