C语言 memcpy 和指针
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5512234/
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
memcpy and pointers
提问by Avinash
I am confuse on how to read the pointers copied in an array using memcpy. Following is what I have tried, but does not work.
我对如何使用 memcpy 读取在数组中复制的指针感到困惑。以下是我尝试过的,但不起作用。
Basically, I have allocated block of memory in which I am copying pointers similar to array fashion, but during retrial it is not working. While this works with basic data types properly
基本上,我已经分配了内存块,我在其中复制类似于数组方式的指针,但是在重试期间它不起作用。虽然这适用于基本数据类型
I want to store anything in the elementblock, It can be either integersor pointers.
我想在element块中存储任何东西,它可以是integers或pointers。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INDEX(x) ((char *)elements + (sizeof(int*) * (x)))
int size = 10;
void f2_int_ptr() {
int i = 0;
void *elements = (void*)malloc(size * sizeof(int*));
for ( i = 0; i < size; i++ ) {
int *v = ( int *) malloc ( sizeof(int));
memcpy ( v, &i, sizeof ( int ));
memcpy ( INDEX(i) , v, sizeof (int*));
}
for ( i = 0; i < size; i++ ) {
int *v = (int*)0;
memcpy ( v, INDEX(i), sizeof(int *));
printf ( "%d\n", *v );
}
}
void f1_int() {
int i = 0;
void *elements = (void*)malloc(size * sizeof(int));
for ( i = 0; i < size; i++ ) {
memcpy ( INDEX(i) , &i, sizeof (int));
}
for ( i = 0; i < size; i++ ) {
int v;
memcpy ( &v, INDEX(i), sizeof ( int ));
printf ( "%d\n", v );
}
}
int main(){
f1_int();
f2_int_ptr();
return 0;
}
In the above code f1_intworks fine but f2_int_ptrdoes not work.
在上面的代码中f1_int工作正常但f2_int_ptr不起作用。
采纳答案by Oliver Charlesworth
f2_int_ptr()needs to become this:
f2_int_ptr()需要变成这样:
void f2_int_ptr() {
int i = 0;
void *elements = malloc(size * sizeof(int*));
for ( i = 0; i < size; i++ ) {
int *v = malloc ( sizeof(int));
memcpy ( v, &i, sizeof ( int ));
memcpy ( INDEX(i) , &v, sizeof (int*));
}
for ( i = 0; i < size; i++ ) {
int *v;
memcpy ( &v, INDEX(i), sizeof(int *));
printf ( "%d\n", *v );
}
}
Note the subtle changes to the memcpy()arguments.
注意memcpy()参数的细微变化。
Note: I really, really, really, wouldn't write code like this! It's incredibly difficult to follow.
注意:我真的,真的,真的,不会写这样的代码!遵循它是非常困难的。
回答by sickgemini
If you are storing the pointers in the elemnts, I think the final memcpy needs to use &v to copy the element to the pointer v. Similar to v=elements[i].
如果将指针存储在元素中,我认为最终的 memcpy 需要使用 &v 将元素复制到指针 v。类似于 v=elements[i]。
void f2_int_ptr() {
int i = 0;
void *elements = (void*)malloc(size * sizeof(int*));
for ( i = 0; i < size; i++ ) {
int *v = ( int *) malloc ( sizeof(int));
memcpy ( v, &i, sizeof ( int *));
memcpy ( INDEX(i) , &v, sizeof (int*));
}
for ( i = 0; i < size; i++ ) {
int *v = (int *)0;
memcpy ( &v, INDEX(i), sizeof(int *));
printf ( "%d\n", *v );
}
}
回答by Avinash
thanks Guys, it worked finally. i guess i have not allocated the space where to copy memcpy.
谢谢大家,终于成功了。我想我还没有分配复制 memcpy 的空间。
void f2_int_ptr() {
int i = 0;
void *elements = (void*)malloc(size * sizeof(int*));
for ( i = 0; i < size; i++ ) {
int *v = ( int *) malloc ( sizeof(int));
memcpy ( v, &i, sizeof ( int ));
memcpy ( INDEX(i) , v, sizeof (int*));
}
for ( i = 0; i < size; i++ ) {
int *v = ( int *) malloc (sizeof(int*));
memcpy ( v, INDEX(i), sizeof(int*));
printf ( "%d\n", *v );
}
}
回答by IProblemFactory
Code is very ugly, so I dont even know how it should works but: Why are u using &v here:
代码非常难看,所以我什至不知道它应该如何工作,但是:你为什么在这里使用 &v:
memcpy ( &v, INDEX(i), sizeof ( int ));
v is pointer itself:
v 是指针本身:
int *v = (int*)0;

