Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Difference between delete and free() in C++?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 976
    Comment on it

    free() function and delete operator are used to deallocate the memory a pointer is holding.

    free() function is used when the memory is allocated to the pointer by either malloc(),calloc() or realloc() function, whereas delete operator is used when the memory is allocated to the pointer using new operator.

    For example;

    int main()
    {
        /* free() is used to free memory assigned with malloc() or calloc() */
        int *pointer1 = (int *)malloc(sizeof(int));
        int *pointer2 = (int*) calloc (10,sizeof(int));
        free(pointer1); 
        free(pointer2);  
    
        /*  delete is used to free memory assigned with new operator */
        int *pointer3 = new int;
        delete pointer3;
    
        getchar();
        return 0;
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: