Everybody knows that you have to free()
pointers, when you use malloc()
because the memory is allocated in the heap, which is not kept account of by the process.
But why don't I have to use free()
when assigning a pointer without a heap:
char* text0 = calloc(20, sizeof (char));
strncpy(text0, "Hello World", 20); // Remember to use `free()`!
char* text1 = "Goodbye World!"; // No need for `free()`?
Isn't text1
also a pointer on the stack pointing to the memory allocated on the heap? Why is there no need for free()?