Suppose I have the following program
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char *returnSomething()
{
char *myString;
myString = malloc(sizeof(char) * 50);
strcpy(myString,"hello");
free(myString);
return myString;
}
int main(int argc, char const *argv[])
{
char *myString = returnSomething();
printf("%s",myString);
return 0;
}
Why is it that this will successfully print "hello" when I free'd it before returning? I thought it woudn't print anything since I free'd the memory then returned the string afterwards. I assumed I always had to free it in main after printing it.
Is this just my mac compiler being nice?