free() is correctly releasing the memory. 1
What does not fit you is that
malloc , when allocating memory, does not initialize . It gives you a pointer to memory and the "garbage" that is there is maintained.
In your case it seems that the memory manager releases
s and, when you ask for the same amount of memory again, it gives you the same position (which is now available) 2 . If you did not free the memory,
malloc would return a pointer to any other zone , and not to it.
The real problem is reading the file. Your code goes character by character assigning to the array, but the problem is that when you finish reading the characters you do not put the termination value of
). Therefore, any strings function (
, etc.) will continue reading beyond the characters you have assigned until you find a
Solution. Two options:
The correct one for this case is that, after reading the file, assign
malloc to finish the
0 . Looking at your code, I would:
s[i++] = ch;
s[i] = (char) 0; // En cada iteración "terminas" la cadena para poder
// usar strcmp. Si iteras otra vez, el 0 se sobreescribirá
// con el siguiente caracter.
And more related to memory management, you can call
malloc instead of
; what it does is that apart from assigning you the memory you initialize it all to
It should also be noted that being continuously requesting and releasing memory is very inefficient. If you do not keep a permanent reference to the memory, it would be much better to do
malloc before starting the loop and release it later. As long as you remember to put the %code% to end the %code% , there will be no problem with reusing the same area. Naturally, once you're done, you can only apply the first solution.
1 A simple test. Although releasing a pointer twice means "undefined behavior" and in theory anything can happen, in my experience it almost always means an abrupt termination of the program. You can do the experiment of releasing twice %code% and check that, possibly, the program suffers a crash.
2 As far as I know, %code% is free to return any pointer; that you return the same depends on the implementation.