Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The reason I told you before but now let me tell you more clearly.Remember these arpita, the free() does not work with normal pointers , it only works with the pointers that are involved with malloc(),calloc() and realloc().The free() was developed in a way that it works only with dynamic memory allocation concepts.If you try to use it with a normal pointer it results segmentation fault or core dumped in linux based OS it is because the function is defined in that way because you are trying to free a memory pointed by q but q is uninitialized you don't know the address it is pointing to and you don't even know the size of memory to be deallocate.So their lies the error.I hope you understood. And one more thing people might be saying </p> <p>int main()</p> <p>{</p> <p>int a=45;</p> <p>int *q=&a;</p> <p>free(q);</p> <p>}</p> <p>Some people may think and ask the above question saying that the q is initialized in the above code it is pointing to a and now why free() is not woking.Because i already told you the free() works only with dynamic memory allocation function. And in the above you are pointing to int a, but notice that int a, is a statically allocated variable. Hence still the error exists.</p> <p>However when you move to some other language you will be having delete operator to delete it even if it not involved in dynamic allocation.People bring me a notification that the above code works . and i say yes with some compilers but that compilers are not for the real applications.I suggest you to work on gcc compiler,</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload