Dangling pointer

Posted by viswanathan on Stack Overflow See other posts from Stack Overflow or by viswanathan
Published on 2010-06-06T05:01:15Z Indexed on 2010/06/06 5:02 UTC
Read the original article Hit count: 225

Filed under:

Does this piece of code lead to dangling pointer. My guess is no.

class Sample
{
public:
int *ptr;
Sample(int i)
{
ptr = new int(i);
}

~Sample()
{
delete ptr;
}
void PrintVal()
{
cout << "The value is " << *ptr;
}
};

void SomeFunc(Sample x)
{
cout << "Say i am in someFunc " << endl;
}

int main()
{
Sample s1 = 10;
SomeFunc(s1);
s1.PrintVal();
}

© Stack Overflow or respective owner

Related posts about c++