We had a long debat and still no answer to this question related to the leaks in C programming, Here is a small example :
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
int main(int argc, char **argv)
{
char *s;
if (s = (char *)malloc(7 + 1) == NULL)
exit(1);
s = strcpy(s, "bonjour");
printf("%s\n", s);
if (argc == 2)
exit(1);
else if (argc == 3)
printf("I didn't free and exit normaly");
else
free(s);
return (0);
}
Here if we do a simple
gcc leaks.c -o leaks
./leaks
will free and programme exit normaly
./leaks exit
will exit without free
./leaks "nornamly" "exit wihtout free"
will normaly exit but without free
which one of those three programme is the worst and the best. Well obviously the first is the best. but what about the two other ?
Do we really need to free as the programme exit just after and memory will be getting back by the OS ?
Thanks you everyone for this big question.