Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy values from one pointer to another

Tags:

I have the following pointer:

jfloat *verticesLocal; 

And I want make a new copy to:

jfloat *vertices; 

I want to copy the values from verticesLocal to vertices.

How can I do that? I've just tried the following command, but it doesn't work:

memcpy(vertices, verticesLocal, numVerticesLocal * sizeof(jfloat)); 

I can't see the error because I'm working with Android native code. Sorry.

like image 984
VansFannel Avatar asked Dec 10 '10 16:12

VansFannel


People also ask

Can pointer be copied?

Copying a pointer does not copy the corresponding object, leading to surprises if two pointers inadvertently points to the same object. Destroying a pointer does not destroy its object, leading to memory leaks.

Can we assign a value directly to a pointer?

You can assign 0 into a pointer: ptr = 0; The null pointer is the only integer literal that may be assigned to a pointer.

Can we assign a pointer variable to another pointer variable?

A pointer is associated with a type (of the value it points to), which is specified during declaration. A pointer can only hold an address of the declared type; it cannot hold an address of a different type.


1 Answers

The idea of "copying a pointer", when taken literally, is nothing more than a simple assignment.

int x = 5; int* p1 = &x; int* p2 = p1; // there, we copied the pointer. 

In this case, both p1 and p2 point to the same data - the int variable x. However, because this is so trivial, I'm inclined to think that what you really are asking about is how to copy the data that the pointer points to.

In this case, it depends on the data type. If the pointer points to a simple buffer of PODs, this involves allocating a separate buffer, and then using something like memcpy (or preferably std::copy) to copy the data. For example:

int* p1 = new int[100]; // ... fill p1 with values int* p2 = new int[100]; // create a new buffer std::copy(p1, p1 + 100, p2); // copy the data into p2 

Or, you can use memcpy to copy the buffer byte-by-byte, since the buffer contains PODs.

memcpy(p2, p1, 100 * sizeof(int)); 

However, if the pointed-to data is not a simple buffer, but rather a C++ object, you can't use memcpy. You need to perform a deep copy of the object, (usually using the object's copy constructor) to get a clone of the object. How this is done, or whether it's even possible, depends on the object. (Some objects are noncopyable.)

I have no clue what a jfloat is, but if the object is, for example, an std::string, you would just do something like:

std::string* s1; // assume this points to a valid string std::string* s2 = new std::string(); *s2 = *s1; // copies s1 using s2's assignment operator 

In this contrived example it would be preferable to avoid heap-allocation altogether, and just use stack variables. But it demonstrates the idea of copying a heap-allocated object.

like image 57
Charles Salvia Avatar answered Sep 19 '22 15:09

Charles Salvia