Free dynamically allocated memory
There is no memory leak in your code. As for your second question, there malloc
is more going on in the call than just returning memory to you. The C library reserves space to place its own headers and accounting information so that when called, free
it can do the right thing.
Some editorial notes on your code:
-
you don't need to specify the return value
malloc()
in a C program. Conversions betweenvoid *
(which itmalloc()
returns) and other pointer types are implicit in C. -
sizeof(char)
1
why write it? -
your loop writes three characters to
s
and then your program skips the character (s[4]
) before adding\0
. It's a bit strange. Did you mean to use iti <= 4
in your loop or maybes[4] = '\0'
after it? -
after the call
free()
you have.
, not;
. I'm guessing it's just a typo here and not in your program as it won't compile that way.
source to share