What does delete do in c
The following two cases produce undefined results: using the array form of delete delete [] on an object, and using the nonarray form of delete on an array. For examples of using delete , see new operator. For objects not of class type class , struct , or union , the global delete operator is invoked.
For objects of class type, the name of the deallocation function is resolved in global scope if the delete expression begins with the unary scope resolution operator Otherwise, the delete operator invokes the destructor for an object prior to deallocating memory if the pointer is not null. The delete operator can be defined on a per-class basis; if there is no such definition for a given class, the global operator delete is invoked.
If the delete expression is used to deallocate a class object whose static type has a virtual destructor, the deallocation function is resolved through the virtual destructor of the dynamic type of the object.
Expressions with Unary Operators Keywords new and delete Operators. Feedback will be sent to Microsoft: By pressing the submit button, your feedback will be used to improve Microsoft products and services. Privacy policy. Skip to main content. Article Contributed By :.
Abhishek rajput. Easy Normal Medium Hard Expert. Writing code in comment? Please use ide. Load Comments. What's New. We use cookies to ensure you have the best browsing experience on our website. Ethical Hacking. Computer Graphics. Software Engineering. Web Technology. Cyber Security. C Programming. Control System. Data Mining. Data Warehouse. Javatpoint Services JavaTpoint offers too many high quality services.
Syntax of free function Suppose we have declared a pointer 'ptr', and now, we want to de-allocate its memory: free ptr ; The above syntax would de-allocate the memory of the pointer variable 'ptr'. If the pointer is null, then the free function will not do anything.
If the pointer is allocated using malloc, calloc, or realloc, but not pointing to any memory block then this function will cause undefined behavior. Its main function is to free the memory.
Let's understand through an example. Output Let's see how free function works with a calloc. Output: Let's look at another example. To delete the pointer, we use the following statement: delete p; To delete the array, we use the statement as given below: delete [] p; Some important points related to delete operator are: It is either used to delete the array or non-array objects which are allocated by using the new keyword.
To delete the array or non-array object, we use delete[] and delete operator, respectively. The new keyword allocated the memory in a heap; therefore, we can say that the delete operator always de-allocates the memory from the heap It does not destroy the pointer, but the value or the memory block, which is pointed by the pointer is destroyed.
Let's look at the simple example of a delete operator. Output Let's see how delete works with an array of objects.
0コメント