I'm new to C.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void demo() {
char* s = malloc(10);
strcpy(s, "foo");
free(s);
}
int main()
{
demo();
}
Will this program leak memory with several bytes?
Will this program leak memory with several bytes?
No.
free(s)
will de-allocate all the memory that was dynamically allocated by char* s = malloc(10);
in this example.
As @Alexander commented: "Is your concern about the last 6 characters of s
? (past the end of "foo"
) Worry not, free()
will free the whole allocation of s
, regardless of how you used it or what you put in it"
Good read: What is a memory leak?