473,326 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

About wild pointer


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 );
}
---
Posted via news://freenews.netfront.net
Complaints to ne**@netfront.net
Jul 19 '05 #1
4 5362
In article <bf***********@adenine.netfront.net>, yangyong wrote:
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.

--
Wellu Mäkinen <we***@NOSPAMwellu.org>
gpg_key http://www.wellu.org/key.pgp
No tears please, it's a waste of good suffering.
Jul 19 '05 #2
"yangyong" <ya******@xteamlinux.com.cn> wrote in message
news:bf***********@adenine.netfront.net
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.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 19 '05 #3


yangyong wrote:


Patient: "Doctor, if I do this, it hurts"
Doctor: "Don't do it"

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #4
"yangyong" <ya******@xteamlinux.com.cn> wrote in message news:<bf***********@adenine.netfront.net>...
[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
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: fighter1 | last post by:
Hello, I have a question about passing an object into a constructor. How can I check to make sure that the object is good/valid that is being passed into the constructor. Obviously I could...
1
by: Kelvin | last post by:
hi everyone... i discover this accentally when i was debugging a program... here is the problem( or maybe not :) ) *************CODE******************* #include <iostream> using namespace...
19
by: JustSomeGuy | last post by:
I have a class that has a static member variable. string x; x should never change during use and should be intialized to "abcd". How does one do this?
2
by: Pawe³ | last post by:
Hello! I'm looking for efficient code or site where I can find code for finding one string in another string. String which I search should have "wild" characters like '?' for any one char and...
50
by: Joseph Casey | last post by:
Greetings. I have read that the mistake of calling free(some_ptr) twice on malloc(some_data) can cause program malfunction. Why is this? With thanks. Joseph Casey.
23
by: Ray | last post by:
Hello! I've been reading about PyPy, but there are some things that I don't understand about it. I hope I can get some enlightenment in this newsgroup :) First, the intro: <excerpt> "The...
7
by: Micheal | last post by:
Hi, I want to know about news groups of visual studio.net 2005 . regards micheal
8
by: Ivan Liu | last post by:
Hi, I'm wondering how to make sure a reference doesn't refer to nothing after the pointer of the value it refers to is deleted. For example, BigCat * pLucky = new BigCat; BigCat & rCat =...
7
by: W. eWatson | last post by:
Is it possible to do a search for a wild card string in another string. For example, I'd like to find "v*.dat" in a string called bingo. v must be matched against only the first character in bingo,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.