Anything Wrong With This Code


  • 26 Sep, 2012

    Here, you have declared a pointer named "p" and initialized that with a "0". After that you are using the delete operator (without using dynamic memory new operator)that is meant to be used after the new operator.

    You should use this:

    int *p;            // declared a new pointer

    p = new int;  // allocate a dynamic memory through this pointer

    *p = 0;        // assign value to the dynamic memory

    and then,

    delete p;    // after using pointer, finally you have to deallocate that memory

    p = NULL;  // and finally pointer should be assign a NULL

     

    Hope it would help a bit.

    Regards.

    Comments Received:
    Please give your suggestions and feedback:
  • 11 Mar, 2012

    T *p = 0;delete p;Yes, the program will crash in an attempt to delete a null pointer.

    Comments Received:
    Please give your suggestions and feedback:

2009-2016 downloadmela.com. All rights reserved.