1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

deleting dangling pointers

Discussion in 'C++' started by maharjun, Sep 30, 2010.

  1. maharjun

    maharjun New Member

    Sep 30, 2010
    Likes Received:
    Trophy Points:
    if a pointer is a dangling pointer and one applies delete[] to it. is the result defined or not. i basically have the following situation

    class A
    int *p;
    //other data members
    //rest of class
    i intend to initialise p in the constructors. but we know that constructors are also called when the = operator is encountered(or atleast thats what i observed). so when we get the arguments from an = assignment, it is possible that p already points to some valid location and hence, while reassigning the class, would have to be deleted to avoid memory leak.

    but when the constructor is first called i.e. when object is created, at that time the pointer would not point to anything valid neither would it be NULL. hence there is a very high chance that the delete will execute even if i bracket it with an if check to check for null pointer.

    hence it would mean that i am applying the delete operator on a wild/dangling pointer. will this lead to any instability etc.?

    i know that this can be averted if i define the = operators. but im looking for ather options(if exist).

    thanks for help.

Share This Page