Using c:
char ptr[n];
free(ptr);
In my opinion: when “char ptr[n];” is used, the memory is allocated, and ptr is pointed to it, free(ptr) should work.
And the program failed, why?(n == 5 e.g.)
Any deep analysis?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Because you called
freeon a variable not allocated withmalloc.This causes Undefined Behavior. Luckily for you it crashes and you can detect it, else it can crash at most awkward times.
You call
freefor deallocating memory of heap allocated variables, What you have is an array on local storage(assuming it to be in a function) and it automatically deallocates when the scope({,}) in which it was created ends.