Dangling Pointer issue

Discussion in 'C++' started by sg, Apr 14, 2011.

  1. sg

    sg Guest

    In the below code

    when we are passing by value in the function
    SomeFunc(CDanglingPointers x) why the local object is not created?


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

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

    void SomeFunc(CDanglingPointers x) => when we are passing by value why
    the local object is not created?
    {
    cout << "Say i am in someFunc " << endl;
    }

    int main()
    {
    CDanglingPointers s1 = 10;
    SomeFunc(s1);
    s1.PrintVal();
    }
    sg, Apr 14, 2011
    #1
    1. Advertising

  2. * sg, on 14.04.2011 19:21:
    > In the below code
    >
    > when we are passing by value in the function
    > SomeFunc(CDanglingPointers x) why the local object is not created?
    >
    >
    > class CDanglingPointers
    > {
    > public:
    > int *ptr;
    > CDanglingPointers(int i)
    > {
    > ptr = new int(i);
    > }
    >
    > ~CDanglingPointers()
    > {
    > delete ptr;
    > }
    > void PrintVal()
    > {
    > cout<< "The value is "<< *ptr;
    > }
    > };
    >
    > void SomeFunc(CDanglingPointers x) => when we are passing by value why
    > the local object is not created?
    > {
    > cout<< "Say i am in someFunc "<< endl;
    > }
    >
    > int main()
    > {
    > CDanglingPointers s1 = 10;
    > SomeFunc(s1);
    > s1.PrintVal();
    > }


    First, please indent your code systematically.

    Now, this looks like homework, so I'm not going to a detailed answer.

    Rather, I'll just you pointers that you can use to get a grip on these things.

    Main pointer: the code is in violation of the "rule of three" (google it), and
    has Undefined Behavior.

    When you pass the object by value, its copy constructor is invoked to copy the
    object. Since you have not defined a copy constructor, the default automatically
    generated one is used. And that one violates the "rule of three".


    Cheers & hth.,

    - Alf

    --
    blog at <url: http://alfps.wordpress.com>
    Alf P. Steinbach /Usenet, Apr 14, 2011
    #2
    1. Advertising

  3. sg

    Noah Roberts Guest

    On 4/14/2011 10:21 AM, sg wrote:
    > In the below code
    >
    > when we are passing by value in the function
    > SomeFunc(CDanglingPointers x) why the local object is not created?
    >
    >
    > class CDanglingPointers
    > {
    > public:
    > int *ptr;
    > CDanglingPointers(int i)
    > {
    > ptr = new int(i);
    > }
    >
    > ~CDanglingPointers()
    > {
    > delete ptr;
    > }
    > void PrintVal()
    > {
    > cout<< "The value is "<< *ptr;
    > }
    > };


    Rule of Big Three violation.


    --
    http://crazycpp.wordpress.com
    Noah Roberts, Apr 14, 2011
    #3
  4. sg

    sg Guest

    I understand that....

    but yet the local object should get created. But it would not do the
    allocation for member int* ptr.

    but another set of CTOR & DTOR shd get invoked.

    Regards
    Sandeep
    sg, Apr 15, 2011
    #4
  5. On 4/15/2011 4:53 AM, sg wrote:
    > I understand that....


    You understand WHAT?

    > but yet the local object should get created. But it would not do the
    > allocation for member int* ptr.


    But yet you seem to forget to check what has been recommended TWICE to
    you: get familiar with "The Rule of Three", and make sure your code
    adheres to that rule.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Apr 15, 2011
    #5
  6. sg

    Guest

    On Apr 14, 1:21 pm, sg <> wrote:
    > In the below code
    >
    > when we are passing by value  in the function
    > SomeFunc(CDanglingPointers x) why the local object is not created?
    >
    > class CDanglingPointers
    > {
    > public:
    > int *ptr;
    > CDanglingPointers(int i)
    > {
    > ptr = new int(i);
    >
    > }
    >
    > ~CDanglingPointers()
    > {
    > delete ptr;}
    >
    > void PrintVal()
    > {
    > cout << "The value is " << *ptr;
    >
    > }
    > };
    >
    > void SomeFunc(CDanglingPointers x) => when we are passing by value why
    > the local object is not created?
    > {
    > cout << "Say i am in someFunc " << endl;
    >
    > }
    >
    > int main()
    > {
    > CDanglingPointers s1 = 10;
    > SomeFunc(s1);
    > s1.PrintVal();
    >
    >
    >
    > }- Hide quoted text -
    >
    > - Show quoted text -


    A local copy *does* get created. In this case, the copy is created
    using a copy constructor. Since you did not define a copy
    constructor, the compiler generated one. The compiler generated copy
    constructor almost always doesn't do the correct thing when you have
    raw pointer data members. This usually true of the assignment
    operator too.

    Here's a sample copy constructor implementation:

    CDanglingPointers(const CDanglingPointers & rhs)
    {
    ptr = new int(*rhs.ptr);
    }

    The assignment operator is left as an exercise.

    HTH
    , Apr 15, 2011
    #6
  7. sg <> wrote:
    > class CDanglingPointers
    > {
    > public:
    > int *ptr;
    > CDanglingPointers(int i)
    > {
    > ptr = new int(i);
    > }


    Remember this simple rule of thumb (emphasis on the word *rule*):
    If your class allocates memory for itself (to a raw pointer member), then
    it *must* have a copy constructor and assignment operator explicitly
    declared, else it's a broken class. No exceptions. If nothing else, declare
    them in the private section of your class (without an implementation).

    Why? Because if you don't, as soon as you assign objects of that class,
    your program will break.
    Juha Nieminen, Apr 17, 2011
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rootz anabo
    Replies:
    0
    Views:
    453
    rootz anabo
    Feb 3, 2005
  2. Hans Van den Eynden

    dangling reference

    Hans Van den Eynden, Oct 16, 2004, in forum: Java
    Replies:
    1
    Views:
    2,847
    Joona I Palaste
    Oct 16, 2004
  3. John
    Replies:
    11
    Views:
    590
    Sharad Kala
    Nov 5, 2004
  4. __PPS__
    Replies:
    20
    Views:
    856
    __PPS__
    Oct 16, 2005
  5. Jarek Blakarz
    Replies:
    2
    Views:
    329
    Jarek Blakarz
    Nov 6, 2012
Loading...

Share This Page