473,326 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

help: copy of pointer & values

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

Jul 22 '05 #1
4 2218
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
Jul 22 '05 #2

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[i] = t[i], 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[i] = vfrom[i]; " ?

xuatla
Jul 22 '05 #3
"xuatla" <xu****@gmail.com> 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[i] = vfrom[i]; " ?


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
Jul 22 '05 #4
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;
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: jhon02148 | last post by:
hi this hw have four files: 1. for the main program 2. listp.cpp (the source file) 3. listp.h (the header file) 4. exception.h if there is anybody who could help me with this hw i really...
1
by: jhon02148 | last post by:
hi this hw have four files: 1. for the main program 2. listp.cpp (the source file) 3. listp.h (the header file) 4. exception.h hi iam done with my hw i still have to do one function which is...
5
by: Tony Johansson | last post by:
Hello! I'm reading in a book about C++ and that is something that sound strange. It says "Pointers have reference-assignment semantics similar to those in Java. For example, after the...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
7
by: simkn | last post by:
Hello, I'm writing a function that updates an array. That is, given an array, change each element. The trick is this: I can't change any elements until I've processed the entire array. For...
38
by: Red Dragon | last post by:
I am self study C student. I got stuck in the program below on quadratic equation and will be most grateful if someone could help me to unravel the mystery. Why does the computer refuse to execute...
4
by: jereme.goblue | last post by:
The web page at http://www.rossowinspections.com/ has an issue with the left menu. The left menu has 7 links. One of the links doesn't work. Using Firefox 1.5 the Fees link isn't a clickable...
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
30
by: Alf P. Steinbach | last post by:
I once suggested in that SomeOne Else(TM) should propose a string value class that accepted literals and char pointers and so on, with possible custom deleter, and in case of literal strings just...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.