About wild pointer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • yangyong

    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 news@netfront.n et
  • Wellu Mäkinen

    #2
    Re: About wild pointer

    In article <bfqjfa$1q87$1@ adenine.netfron t.net>, yangyong wrote:[color=blue]
    > 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 );
    > }[/color]

    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 <wellu@NOSPAMwe llu.org>
    gpg_key http://www.wellu.org/key.pgp
    No tears please, it's a waste of good suffering.

    Comment

    • John Carson

      #3
      Re: About wild pointer

      "yangyong" <yangyong@xteam linux.com.cn> wrote in message
      news:bfqjfa$1q8 7$1@adenine.net front.net[color=blue]
      > Simple like this:
      >
      > {
      > int* p1 = new int( 9 );
      > int* p2 = p1;
      > delete p1;
      > p1 = 0;
      >
      > // how to know p2 now is a wild pointer?
      > }[/color]


      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.


      [color=blue]
      > 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 );
      > }
      >[/color]

      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)

      Comment

      • Karl Heinz Buchegger

        #4
        Re: About wild pointer



        yangyong wrote:[color=blue]
        >[/color]

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

        --
        Karl Heinz Buchegger
        kbuchegg@gascad .at

        Comment

        • puppet_sock@hotmail.com

          #5
          Re: About wild pointer

          "yangyong" <yangyong@xteam linux.com.cn> wrote in message news:<bfqjfa$1q 87$1@adenine.ne tfront.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

          Comment

          Working...