Running valgrind
with line 42 commented out shows that there are indeed some leaks.
If we DON'T comment out line 42, then there are no leaks.
Here is the code
// copy-1.mp4 from Week4 Walkthroughs
// copied using random access into string array
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(void)
{
printf("Enter string: ");
char* s = GetString();
if (s == NULL)
{
return 1;
}
char* t = malloc((strlen(s) + 1) * sizeof(char));
if (t == NULL)
{
free(s);
return 1;
}
// so that i iterate up to and including the null character
for (int i = 0, n = strlen(s); i <= n; i++)
{
t[i] = s[i];
}
printf("Capitalizing copy...\n");
if (strlen(t) > 0)
{
t[0] = toupper(t[0]);
}
printf("Original string: %s\n", s);
printf("Copy of string: %s\n", t);
free(t);
// free(s); // why didn't sir include this line too??
return 0;
}