In the lecture of week 4 in CS50x, David shows how to allocate memory using malloc() to clone a string, and how free() needs to be called whenever malloc() was called to free up allocated memory afterwards.
I can copy a string, change the copy and verify the source wasn't changed, but when I call free() on the copied variable, it removes all its contents.
This is the code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
// ---
int main(void)
{
char *source = "profound words.";
char *destination = malloc(strlen(source) + 1); // length of source plus 1 for \0 character
if (destination == NULL)
{
return 1; // problem allocating memory
}
strcpy(destination, source);
printf("before: source: %s, destination: %s\n", source, destination);
printf("source memory: %p, destination memory: %p\n", &source, &destination);
if (strlen(destination) > 0)
{
printf("uppercasing first letter of variable 'destination'\n");
destination[0] = toupper(destination[0]);
}
// free(destination); // if executed, will scramble destination contents. why?
printf("after: source: %s, destination: %s\n", source, destination);
printf("source memory: %p, destination memory: %p\n", &source, &destination);
}
Outputs:
before: source: profound words., destination: profound words.
source memory: 0x7fff2514aef0, destination memory: 0x7fff2514aee8
uppercasing first letter of variable 'destination'
after: source: profound words., destination: Profound words.
source memory: 0x7fff2514aef0, destination memory: 0x7fff2514aee8
--> all good
However, when uncommenting the line free(destination);
, the last output line would then be "after: source: profound words., destination: ".
Any ideas why that's happening?