Dangling Pointer issue

S

sg

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();
}
 
A

Alf P. Steinbach /Usenet

* 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
 
N

Noah Roberts

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.
 
S

sg

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
 
V

Victor Bazarov

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
 
A

AnonMail2005

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
 
J

Juha Nieminen

sg said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top