Is it OK to free 'void*'?
Yes, it's safe. When allocating memory, the runtime library keeps track of the size of each allocation. When you call free(), it looks up the address, and if it finds an allocation for that address, the correct amount of memory is freed (the block that was allocated at that address).
Yes.
malloc returns void *
and free takes void *
, so some of your casts are meaningless, and you're always freeing a void *
even if you're starting with some other sort of pointer.