You only free
what you have allocated using malloc
(directly or indirectly) or related function (like realloc
).
Attempting to pass a pointer not returned by malloc
will lead to undefined behavior.
That you get a compiler error for delete
in C++ is first and foremost because C and C++ are different languages with different rules.
And remember, an array is an array, not a pointer. Though an array can decay to a pointer to its first element in many situation (like when passing it to a function).
solved Freeing char[] in C gives error and how do C compiler handle that char[]