About wild pointer

Discussion in 'C++' started by yangyong, Jul 25, 2003.

  1. yangyong

    yangyong Guest

    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
    yangyong, Jul 25, 2003
    #1
    1. Advertising

  2. In article <bfqjfa$1q87$>, 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 <>
    gpg_key http://www.wellu.org/key.pgp
    No tears please, it's a waste of good suffering.
    Wellu =?iso-8859-1?Q?M=E4kinen?=, Jul 25, 2003
    #2
    1. Advertising

  3. yangyong

    John Carson Guest

    "yangyong" <> wrote in message
    news:bfqjfa$1q87$
    > 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)
    John Carson, Jul 25, 2003
    #3
  4. yangyong wrote:
    >


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

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Jul 25, 2003
    #4
  5. yangyong

    Guest

    "yangyong" <> wrote in message news:<bfqjfa$1q87$>...
    [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 25, 2003
    #5
    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. SatishPasala

    Removing wild characters from a string

    SatishPasala, Nov 29, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    2,686
    SatishPasala
    Nov 29, 2005
  2. Replies:
    7
    Views:
    2,558
    tom fredriksen
    Mar 14, 2006
  3. Fat Sam

    Help with Wild West Colors

    Fat Sam, Nov 12, 2004, in forum: HTML
    Replies:
    4
    Views:
    2,777
    nice.guy.nige
    Nov 14, 2004
  4. John

    Wild card symbol in name?

    John, Feb 28, 2006, in forum: HTML
    Replies:
    5
    Views:
    635
  5. Kelvin@!!!

    kinda wild ptr??

    Kelvin@!!!, May 6, 2004, in forum: C++
    Replies:
    1
    Views:
    344
    John Harrison
    May 6, 2004
Loading...

Share This Page