help: copy of pointer & values

X

xuatla

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
 
V

Victor Bazarov

xuatla said:
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
 
X

xuatla

Great thanks to Victor :)


Victor said:
xuatla said:
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.

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
 
V

Victor Bazarov

xuatla said:
[...]
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
 
R

Ron Natalie

xuatla said:
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;
 
Joined
Apr 26, 2010
Messages
1
Reaction score
0
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);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top