help: copy of pointer & values

Discussion in 'C++' started by xuatla, Oct 5, 2004.

  1. xuatla

    xuatla Guest

    Hi,

    How to copy a pointer to another pointer?

    Can I do in the following way:

    // START

    double *copyfrom = new double[10];
    double *copyto = new double[10];

    //initialize part for copyfrom
    //...

    // target: copy to value from copyfrom to copyto.
    // AND, copyfrom is useless after that. (like cut/paste)

    double *tempfordelete;

    tempfordelete = copyto;

    copyto = copyfrom;

    copyfrom = tempfordelete;

    delete [] copyfrom;
    delete [] tempfordelete;

    // use copyto
    // .....

    delete [] copyto;

    // END

    Thanks in advance for the help!

    xuatla
     
    xuatla, Oct 5, 2004
    #1
    1. Advertising

  2. xuatla wrote:
    > How to copy a pointer to another pointer?


    A simple assignment should do.

    > Can I do in the following way:
    >
    > // START
    >
    > double *copyfrom = new double[10];
    > double *copyto = new double[10];
    >
    > //initialize part for copyfrom
    > //...
    >
    > // target: copy to value from copyfrom to copyto.
    > // AND, copyfrom is useless after that. (like cut/paste)
    >
    > double *tempfordelete;
    >
    > tempfordelete = copyto;
    >
    > copyto = copyfrom;
    >
    > copyfrom = tempfordelete;


    If what you're doing here is swapping two values, use std::swap:

    std::swap(copyto, copyfrom);

    >
    > delete [] copyfrom;
    > delete [] tempfordelete;


    That's wrong. You didn't get 'tempfordelete' from a 'new[]', so
    why are you deleting it?

    >
    > // use copyto
    > // .....
    >
    > delete [] copyto;
    >
    > // END
    >
    > Thanks in advance for the help!


    What are you trying to accomplish by all this? What problem are
    you trying to solve?

    V
     
    Victor Bazarov, Oct 5, 2004
    #2
    1. Advertising

  3. xuatla

    xuatla Guest

    Great thanks to Victor :)


    Victor Bazarov wrote:
    > xuatla wrote:
    >
    >> How to copy a pointer to another pointer?

    >
    >
    > A simple assignment should do.
    >
    >> Can I do in the following way:
    >>
    >> // START
    >>
    >> double *copyfrom = new double[10];
    >> double *copyto = new double[10];
    >>
    >> //initialize part for copyfrom
    >> //...
    >>
    >> // target: copy to value from copyfrom to copyto.
    >> // AND, copyfrom is useless after that. (like cut/paste)
    >>
    >> double *tempfordelete;
    >>
    >> tempfordelete = copyto;
    >>
    >> copyto = copyfrom;
    >>
    >> copyfrom = tempfordelete;

    >
    >
    > If what you're doing here is swapping two values, use std::swap:
    >
    > std::swap(copyto, copyfrom);
    >
    >>
    >> delete [] copyfrom;
    >> delete [] tempfordelete;

    >
    >
    > That's wrong. You didn't get 'tempfordelete' from a 'new[]', so
    > why are you deleting it?


    I thought to make sure that I free all the memory allocated and I
    thought "delete[] a" doesnt hurt even if a is empty. Now I know I don't
    need to do this.

    >
    >>
    >> // use copyto
    >> // .....
    >>
    >> delete [] copyto;
    >>
    >> // END
    >>
    >> Thanks in advance for the help!

    >
    >
    > What are you trying to accomplish by all this? What problem are
    > you trying to solve?


    In fact this is part of my problem and I stated in another way in
    another post "reference to pointer".

    I have a class "myType", it contains a private member data "double
    *elem". But the arguments in the function I use is "double *", not
    "myType". A fool way I did before is I change the function
    from
    func( int n, double *v );
    to
    newfunc( myType& t )
    {
    double *v;
    // copy t.ele to v, since t.ele is not public,
    // I need to use v = t, blahblah

    func( t.getsize(), v );
    }

    That's why I was thinking about pointer copy. And I was worried about
    the delete[] part, since in the deconstruction function of myType, elem
    will be deleted.

    Then I thought about another way, as I wrote and asked in that post. I
    add a member function to class,
    double*& getele( ) { return elem; } ;
    and then the version will be simple:
    func( t.getsize(), t.getele() );

    I think this is a bad habit since I should "protect" the data in the
    class, esp the pointer in it. But this looks easy in implementation.

    >
    > V


    Thank you very much for your help!

    Is there any fast way to copy the data from one pointer (array) to
    another one? or I have to use "for ( i....) vto = vfrom; " ?

    xuatla
     
    xuatla, Oct 6, 2004
    #3
  4. "xuatla" <> wrote...
    > [...]
    > Is there any fast way to copy the data from one pointer (array) to another
    > one? or I have to use "for ( i....) vto = vfrom; " ?


    If it's just an array of POD (plain old data, look it up), then you
    can use memmove. If it's an array of objects that are not necessarily
    POD, then you need to have an array. You could, of course, use the
    standard 'copy' function:

    std::copy(vfrom, vfrom + n, vto);

    Also, reading your posts (and posts other people) and seeing all the
    confusion dynamic memory causes, I'd strongly recommend putting it
    aside until you feel more confident with different C++ constructs in
    favour of standard containers. std::vector<double> is much easier
    to deal with than double*.

    Victor
     
    Victor Bazarov, Oct 6, 2004
    #4
  5. xuatla

    Ron Natalie Guest

    xuatla wrote:
    > Hi,
    >
    > How to copy a pointer to another pointer?


    You first have to realize that pointers are not arrays.
    Pointers are just addresses of single objects. If you copy
    a pointer, you are just copying the value of a single address.

    Frankly, at this point in your knowledge, it's best you forget
    about pointers if you want to deal with arrays. Use a class
    like vector and its iterators.


    > double *tempfordelete;
    >
    > tempfordelete = copyto;
    >
    > copyto = copyfrom;
    >
    > copyfrom = tempfordelete;
    >
    > delete [] copyfrom;
    > delete [] tempfordelete;
    >
    > // use copyto
    > // .....
    >
    > delete [] copyto;
    >
    >

    The above is all wrong. Copying a pointer has no effect on to what the
    pointer points. In your case lets refer to the values that originally
    allocate as NEW1 (orignally assigned to copyfrom) and NEW2 (assigned to
    copyto).

    You successfully interchange copyfrom and copyto (although std::swap
    would have been clearer). You then delete the NEW2 value (retrieved
    from copyfrom) and then delete the NEW2 value (from tempfordelte) which
    is undefined behavior.

    All you need do was
    delete [] copyto; // get rid of what *copyto
    copyto = copyfrom;
     
    Ron Natalie, Oct 6, 2004
    #5
  6. xuatla

    FS2009

    Joined:
    Apr 26, 2010
    Messages:
    1
    Hope this can help.

    void CopyLinkList(CPtrList *pDestList, CPtrList *pSrcList, int size)
    {
    // Always check the validity of pointer list source
    if (!pSrcList)
    return;

    // Copy from source to destination
    for (POSITION pos = pSrcList->GetHeadPosition(); pos != NULL;)
    {
    LPVOID pNode = pSrcList->GetNext(pos);
    if(IsBadReadPtr(pNode, 1)) break;

    char * pChar = (char*)malloc(size);
    memcpy(pChar, pNode, size);
    pDestList->AddTail(pChar);
    }
    }
     
    FS2009, Apr 26, 2010
    #6
    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. Replies:
    30
    Views:
    1,190
  2. Alex
    Replies:
    2
    Views:
    1,232
  3. Replies:
    26
    Views:
    2,118
    Roland Pibinger
    Sep 1, 2006
  4. Replies:
    11
    Views:
    998
    Nindi
    Nov 14, 2006
  5. pereges
    Replies:
    7
    Views:
    438
    pereges
    Jun 1, 2008
Loading...

Share This Page