In the shown example can cause trouble. It’s called dangling Pointer.
The free()
is acting on the variable palavra
that has not been initialized. That is, in C has a value in it, you have no idea what that value is, it will vary in each execution of the generated application. He’ll try to release the memory pointed at that address. If you’re lucky you can just fail and nothing happens or you can release something that was allocated and shouldn’t. Something disastrous can happen and even displace things completely outside of what is expected, since even the size of the allocation can be considered erroneously by free()
. The information is virtually random.
In C we have to be very careful. One of these precautions is always initialize the variable. This would cause the free()
wrong did not cause problems, although the code would not be strictly correct. The ideal is not to leave a free()
free so (did not resist :) ). Will one day someone change something and it happens to be a problem even in cases where it was not initially.
Give an example of such use.
– Maniero
There’s no point, just to give an example of what I meant
– Lucas Fernandes