About wild pointer

Y

yangyong

Simple like this:

{
int* p1 = new int( 9 );
int* p2 = p1;
delete p1;
p1 = 0;

// how to know p2 now is a wild pointer?
}

Or :

class a1{
public:
int i;
}

void fun1( a1 * p1)
{
int i = p1->i;
delete p1;
p1 = 0;
}

void fun2( a1* p1)
{
int i = p1->i;
}

void main()
{
a1* p1 = new a1;
a1* p2 = p1;
fun1( p1 );
// how to know p2 now is a wild pointer?
fun2( p2 );
}
 
W

Wellu =?iso-8859-1?Q?M=E4kinen?=

Or :

class a1{
public:
int i;
}

void fun1( a1 * p1)
{
int i = p1->i;
delete p1;
p1 = 0;
}

void fun2( a1* p1)
{
int i = p1->i;
}

void main()
{
a1* p1 = new a1;
a1* p2 = p1;
fun1( p1 );
// how to know p2 now is a wild pointer?
fun2( p2 );
}

From the API documentation. The function should be
documented so that the caller knows what happens to the
passed argument.


BTW. This has nothing to do with pure C++ but in Symbian
(www.symbian.com) platform this is handled so that if
function takes arguments as pointers it refers that the
function will take the ownership of passed objects and is
responsible for deleting those objects as well. In 'normal'
case you pass asguments as references and the ownership
won't be transferred.
 
J

John Carson

yangyong said:
Simple like this:

{
int* p1 = new int( 9 );
int* p2 = p1;
delete p1;
p1 = 0;

// how to know p2 now is a wild pointer?
}


Precisely because of the problem of "wild pointers", as you call them,
having two pointers pointing to the same memory is considered bad
programming practice in most cases. As far as I know, there is no
platform-independent way of checking the validity of pointers and, even if
there was, it would be easy to forget to do so. Don't have two pointers
pointing to the same memory and you will avoid the problem.


Or :

class a1{
public:
int i;
}

void fun1( a1 * p1)
{
int i = p1->i;
delete p1;
p1 = 0;
}

void fun2( a1* p1)
{
int i = p1->i;
}

void main()
{
a1* p1 = new a1;
a1* p2 = p1;
fun1( p1 );
// how to know p2 now is a wild pointer?
fun2( p2 );
}

This is a slightly more subtle case, but the answer is similar. Having
functions that delete memory is generally regarded as bad practice
(basically, memory should be allocated in constructors and deleted in
destructors). But if you must use such functions, then you just have to
document what they do and program very carefully.
 
P

puppet_sock

[wild pointers, what do do?]

As others have pointed out, there is no standard way to examine
a pointer and know it is or is not valid. Thus, you must do the
work yourself in your code. How you do that depends on a lot of
things.

At one level, you just never make a memory location invalid until
you are done with it. So, in broad strokes, it looks like so:

- get the memory
- a lot of stuff happens
- give the memory back
- no more references to the memory

Sometimes it is hard to arrange your code this way. One trick
to make it easier is to restrict the places you use pointers.
For example: You could make every pointer a data member of a
class, and put lots of guardian code in the class. You can then
get into various things like smart pointers, reference counts,
and so on. How sophisticated you make this depends on how
critical it is to be able to demonstrate you don't have any
wild pointers in your code. And how much overhead, in terms of
code and execution time, is acceptable.

Smart pointers and reference counts get discussed here fairly
frequently. There is also some discussion of them in the FAQ.
Search back through google archives to find something applicable
to your particular task.

Another possible choice is to reduce the places you use pointers.
For arrays, for example, you should be looking at the standard
container templates, and avoid using arrays you allocate yourself.
The implementers of the standard library have gone to a lot of
work to make it efficient, flexible, etc. You should consider
using it instead of allocating your own arrays.
Socks
 

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