C++ 如何将值从一个指针复制到另一个指针

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4411225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:19:23  来源:igfitidea点击:

How to copy values from one pointer to another

c++pointersandroid-ndk

提问by VansFannel

I have the following pointer:

我有以下指针:

jfloat *verticesLocal;

And I want make a new copy to:

我想制作一个新副本:

jfloat *vertices;

I want to copy the values from verticesLocalto vertices.

我想将值从 复制verticesLocalvertices

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.

我看不到错误,因为我正在使用 Android 本机代码。对不起。

采纳答案by NPE

mallocfirst, then do your memcpy.

malloc首先,然后执行您的memcpy.

回答by Charles Salvia

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 p1and p2point to the same data - the intvariable x. However, because this is so trivial, I'm inclined to think that what you reallyare asking about is how to copy the datathat the pointer points to.

在这种情况下,p1和 都p2指向相同的数据 -int变量x。但是,因为这太琐碎了,我倾向于认为您真正要问的是如何复制指针指向的数据

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:

在这种情况下,它取决于数据类型。如果指针指向一个简单的POD缓冲区,这涉及分配一个单独的缓冲区,然后使用类似memcpy(或最好std::copy)的东西来复制数据。例如:

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 memcpyto copy the buffer byte-by-byte, since the buffer contains PODs.

或者,您可以使用memcpy逐字节复制缓冲区,因为缓冲区包含PODs

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

However, if the pointed-to data is nota simple buffer, but rather a C++ object, you can't use memcpy. You need to perform a deep copyof 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.)

但是,如果指向的数据不是简单的缓冲区,而是 C++ 对象,则不能使用memcpy. 您需要执行对象的深层复制(通常使用对象的复制构造函数)来获取对象的克隆。这是如何完成的,或者它是否可能,取决于对象。(有些对象是不可复制的。)

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

我不知道 ajfloat是什么,但如果对象是,例如, an std::string,你会做这样的事情:

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.

在这个人为的例子中,最好完全避免堆分配,而只使用堆栈变量。但它演示了复制堆分配对象的想法。

回答by Zac Howland

If you are copying the pointer, it is a simple assignment:

如果要复制指针,则是一个简单的赋值:

jfloat* verticesLocal; // assuming is is allocated already
jfloat* newVertices = verticesLocal;

IF you mean you want to copy the data the point points to, you have to allocate the memory for the new block of memory first:

如果您的意思是要复制该点指向的数据,则必须先为新的内存块分配内存:

// assume jfloat* verticesLocal is pointing to a valid memory block of size i

// 假设 jfloat* verticesLocal 指向一个大小为 i 的有效内存块

jfloat* newVertices = new jfloat[i];
memcpy(newVertices, verticesLocal, i * sizeof(jfloat));