Help , why not return a true array?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guest's Avatar

    #1

    Help , why not return a true array?

    The output is:
    1234
    After getline: 1234
    After renew: 1234
    After retnp: ÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝ ÝG
    After getp: ÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝ ÝG
    -----What happen after renew/retnp call?-------
    Why not return a true array?
    -----The code is as followed-------------------
    #include <iostream.h>
    #include <stdlib.h>
    #include <string.h>

    void print(char *,const char *);
    void renew(char *);
    void retnp(char *);
    char* getp(char *);
    int main(int argc, char* argv[])
    {
    char *p = new char[5];
    cin.getline(p,5 );
    print(p,"getlin e");
    renew(p);
    print(p,"renew" );
    retnp(p);
    print(p,"retnp" );
    char * p1;
    p1 = getp(p);
    print(p1,"getp" );
    return 0;
    }
    //print
    void print(char *p,const char *st)
    {
    cout<<"After "<<st<<": "<<p<<endl;
    }
    //renew
    void renew(char *p)
    {
    char *p1 = new char[strlen(p)+1];
    strcpy(p1,p);
    delete [] p;
    p = new char[10];
    strcpy(p,p1);
    }
    //retnp
    void retnp(char *p)
    {
    char *p1 = new char[strlen(p)+1];
    strcpy(p1,p);
    delete [] p;
    p = p1;
    p1 = NULL;
    delete p1;
    }
    //getp
    char* getp(char *p)
    {
    char *p1 = new char[strlen(p)+1];
    strcpy(p1,p);

    return p1;
    }


  • ajk

    #2
    Re: Help , why not return a true array?

    On Fri, 6 Apr 2007 11:21:37 +0800, "<John Wu>" <jo.mice@hotmai l.com>
    wrote:
    >//renew
    >void renew(char *p)
    >{
    char *p1 = new char[strlen(p)+1];
    strcpy(p1,p);
    delete [] p;
    p = new char[10];
    strcpy(p,p1);
    >}
    in this function you are deleting p i.e. the memory that p is pointing
    to. then you set p to point to another memory block however this is
    not returned from the function, p is still pointing to the orignal -
    now - deleted memory block.

    you need to pass ptr to ptr of p if you want to change where it points
    to:

    void renew( char **p )
    {
    ...

    *p = new char[10];
    strcpy( *p, p1 );
    }

    Comment

    • Ian Collins

      #3
      Re: Help , why not return a true array?

      <John Wuwrote:
      The output is:
      1234
      After getline: 1234
      After renew: 1234
      After retnp: ÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝ ÝG
      After getp: ÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝ ÝG
      -----What happen after renew/retnp call?-------
      Why not return a true array?
      I'm not sure what you are trying to do, but your renew and retnp both
      delete the pointer passed in and then do stuff with the *local* copy of
      the pointer.

      So the net effect is to delete p several times and in the case of renew,
      leek ten bytes. p inside the functions is a *copy* of main's p passed
      as a function parameter. If you want to change main's p, you have to
      pass its address. For this, your function signatures would be

      void renew(char*&);
      void retnp(char*&);

      --
      Ian Collins.

      Comment

      • Jim Langston

        #4
        Re: Help , why not return a true array?

        "<John Wu>" <jo.mice@hotmai l.comwrote in message
        news:ev4glg$d03 $1@news.cn99.co m...
        The output is:
        1234
        After getline: 1234
        After renew: 1234
        After retnp: ÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝ ÝG
        After getp: ÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝ ÝG
        -----What happen after renew/retnp call?-------
        Why not return a true array?
        -----The code is as followed-------------------
        #include <iostream.h>
        #include <stdlib.h>
        #include <string.h>
        >
        void print(char *,const char *);
        void renew(char *);
        void retnp(char *);
        char* getp(char *);
        int main(int argc, char* argv[])
        {
        char *p = new char[5];
        cin.getline(p,5 );
        print(p,"getlin e");
        renew(p);
        print(p,"renew" );
        retnp(p);
        For explanation what is wrong with retnp see the comments in the function
        down below. At this point p points to invalid memory (deleted memory, see
        comments in retnp down below) which is why getp is failling.
        print(p,"retnp" );
        char * p1;
        p1 = getp(p);
        print(p1,"getp" );
        return 0;
        }
        //print
        void print(char *p,const char *st)
        {
        cout<<"After "<<st<<": "<<p<<endl;
        }
        //renew
        void renew(char *p)
        {
        char *p1 = new char[strlen(p)+1];
        strcpy(p1,p);
        delete [] p;
        p = new char[10];
        strcpy(p,p1);
        }
        //retnp
        void retnp(char *p)
        p is a local char* that is passed to the function by value.
        {
        char *p1 = new char[strlen(p)+1];
        You point p1 to some memory returned by new.
        strcpy(p1,p);
        You copy from the memory pointed in into your memory allocated with new.
        delete [] p;
        You delete the memory that p was pointing to. This is also the same memory
        location that was passed in, so effects the memory that the passed in
        variable contains (p in mainline).
        p = p1;
        You assign the *local* p the address of p1
        p1 = NULL;
        You assign toe p1 a NULL pointer
        delete p1;
        This has no effect on a null pointer.
        }
        You return, the local variable p is now destroyed. The memory allocated by
        new is lost, you no longer have a ponter to it.

        If you want to change the actual passed in pointer itself, rather than a
        copy of the pointer, then you need to pass either a pointer to the pointer,
        or a reference the pointer. A reference makes more sense.

        Change the function to
        void retnp( char*& p )

        and it should work as you expect, since it is a reference, changing this
        reference to p is the same as changing the original p.
        //getp
        char* getp(char *p)
        {
        char *p1 = new char[strlen(p)+1];
        strcpy(p1,p);
        >
        return p1;
        }
        Same with this one.


        Comment

        • Guest's Avatar

          #5
          Thank u very much!

          Thanks to ajkÏÈÉú¡¢Ian CollinsÏÈÉú¡¢Ji m LangstonÏÈÉú£¬t hank you very much.
          Jim LangstonÏÈÉú explain in detail the way pointer p and *local* p work ,and
          suggest me to pass either a pointer to the pointer,
          or a reference the pointer will work fine.
          I finally try a way to pass the address of the pointer p.It goes well.
          Thank u£¡


          Comment

          Working...