memcpy() 无法正常工作
memcpy() not working correctly
我在用 c 函数复制二维数组时遇到问题。这是代码:
void Add(Lista* list,int** estado, char* sec, int size)
{
if(list->last==NULL)
{
list->last = calloc(1,sizeof(element));
list->last-> secuencia = sec;
list->last->next = NULL;
list->last->prev = NULL;
list->last-> estado = (int**)calloc(size,sizeof(int));
memcpy(&list->last->estado,&estado,size*sizeof(int));
list->cantidad++;
}
else
{
list->last-> next = calloc(1,sizeof(element));
list->last-> next -> secuencia = sec;
list->last->next->next = NULL;
list->last->next->prev = list->last;
list->last =list->last->next;
list->last->estado = (int**)calloc(size,sizeof(int));
memcpy(&list->last->estado,&estado,size*sizeof(int));
list->cantidad++;
}
}
这是结构体 Lista 和 element
typedef struct element
{
char* secuencia;
struct element* next;
struct element* prev;
int** estado;
}element;
typedef struct Lista
{
int cantidad;
element* last;
}Lista;
思路是在Lista中加入"elements",它是returns最后加入的元素的基本列表。问题是列表 returns 中存储的每个元素都相同 "estado" (2D int 数组),如果我修改其中一个元素 estado,则每个元素 estado 都会得到相同的修改。所以,我不知道问题出在哪里,因为 memcpy() 应该复制值,然后使两个数组彼此独立,对吧?
PS: 对不起,如果解释得不好,我会说西班牙语
编辑
因此,我根据对此的回答更改了我的代码:
void Add(Lista* list,int** estado, char* sec, int size)
{
if(list->last==NULL)
{
list->last = calloc(1,sizeof(element));
list->last-> secuencia = sec;
list->last->next = NULL;
list->last->prev = NULL;
list->last-> estado = (int**)calloc(size,sizeof(int));
memcpy(list->last->estado,estado,size*sizeof(int));
list->cantidad++;
}
else
{
list->last-> next = calloc(1,sizeof(element));
list->last-> next -> secuencia = sec;
list->last->next->next = NULL;
list->last->next->prev = list->last;
list->last =list->last->next;
list->last->estado = (int**)calloc(size,sizeof(int));
memcpy(list->last->estado,estado,size*sizeof(int));
list->cantidad++;
}
}
现在我没有 valgrind 错误,但我一直遇到问题(当我修改一个数组时,其他数组也得到修改)
EDIT2
所以,我开始检查代码,在调用 Add() 之前,数组分配不正确(总是将相同的数组传递给函数),修改 EDIT1 解决了我的问题。
PS2:我会深入研究指针,谢谢
您 memcpy
-ing 到指针的地址而不是它指向的内存ing。试试这个:
memcpy(list->last->estado,estado,size*sizeof(int));
如果您想阅读主题,请查看指针中的指针。
此外,正如一些评论所指出的,双指针与二维数组不同。查看 this answer on creating a pointer to a 2D array。你想在你的代码中做类似的事情。 estado
应该是指向双精度数组的指针。
memcpy(list->last->estado,estado,size*sizeof(int));
不 type/size 一致。
estado
是类型 int**
list->last->estado
是类型 int**
,与 estado
相同(好)
size*sizeof(int)
是 size
乘以错误的大小元素。
而不是 int
,它应该是 2 个指针指向的类型。 (int*
)
推荐如下,避免尺寸元素错误。
memcpy(list->last->estado,estado,size*sizeof *(list->last->estado));
calloc()
同样的问题:尺寸错误。也不需要强制转换。
// list->last->estado = (int**)calloc(size,sizeof(int));
list->last->estado = calloc(size, sizeof *(list->last->estado));
// For consistency
// list->last-> next = calloc(1,sizeof(element));
list->last-> next = calloc(1,sizeof *(list->last-> next));
我在用 c 函数复制二维数组时遇到问题。这是代码:
void Add(Lista* list,int** estado, char* sec, int size)
{
if(list->last==NULL)
{
list->last = calloc(1,sizeof(element));
list->last-> secuencia = sec;
list->last->next = NULL;
list->last->prev = NULL;
list->last-> estado = (int**)calloc(size,sizeof(int));
memcpy(&list->last->estado,&estado,size*sizeof(int));
list->cantidad++;
}
else
{
list->last-> next = calloc(1,sizeof(element));
list->last-> next -> secuencia = sec;
list->last->next->next = NULL;
list->last->next->prev = list->last;
list->last =list->last->next;
list->last->estado = (int**)calloc(size,sizeof(int));
memcpy(&list->last->estado,&estado,size*sizeof(int));
list->cantidad++;
}
}
这是结构体 Lista 和 element
typedef struct element
{
char* secuencia;
struct element* next;
struct element* prev;
int** estado;
}element;
typedef struct Lista
{
int cantidad;
element* last;
}Lista;
思路是在Lista中加入"elements",它是returns最后加入的元素的基本列表。问题是列表 returns 中存储的每个元素都相同 "estado" (2D int 数组),如果我修改其中一个元素 estado,则每个元素 estado 都会得到相同的修改。所以,我不知道问题出在哪里,因为 memcpy() 应该复制值,然后使两个数组彼此独立,对吧?
PS: 对不起,如果解释得不好,我会说西班牙语
编辑
因此,我根据对此的回答更改了我的代码:
void Add(Lista* list,int** estado, char* sec, int size)
{
if(list->last==NULL)
{
list->last = calloc(1,sizeof(element));
list->last-> secuencia = sec;
list->last->next = NULL;
list->last->prev = NULL;
list->last-> estado = (int**)calloc(size,sizeof(int));
memcpy(list->last->estado,estado,size*sizeof(int));
list->cantidad++;
}
else
{
list->last-> next = calloc(1,sizeof(element));
list->last-> next -> secuencia = sec;
list->last->next->next = NULL;
list->last->next->prev = list->last;
list->last =list->last->next;
list->last->estado = (int**)calloc(size,sizeof(int));
memcpy(list->last->estado,estado,size*sizeof(int));
list->cantidad++;
}
}
现在我没有 valgrind 错误,但我一直遇到问题(当我修改一个数组时,其他数组也得到修改)
EDIT2
所以,我开始检查代码,在调用 Add() 之前,数组分配不正确(总是将相同的数组传递给函数),修改 EDIT1 解决了我的问题。
PS2:我会深入研究指针,谢谢
您 memcpy
-ing 到指针的地址而不是它指向的内存ing。试试这个:
memcpy(list->last->estado,estado,size*sizeof(int));
如果您想阅读主题,请查看指针中的指针。
此外,正如一些评论所指出的,双指针与二维数组不同。查看 this answer on creating a pointer to a 2D array。你想在你的代码中做类似的事情。 estado
应该是指向双精度数组的指针。
memcpy(list->last->estado,estado,size*sizeof(int));
不 type/size 一致。
estado
是类型 int**
list->last->estado
是类型 int**
,与 estado
相同(好)
size*sizeof(int)
是 size
乘以错误的大小元素。
而不是 int
,它应该是 2 个指针指向的类型。 (int*
)
推荐如下,避免尺寸元素错误。
memcpy(list->last->estado,estado,size*sizeof *(list->last->estado));
calloc()
同样的问题:尺寸错误。也不需要强制转换。
// list->last->estado = (int**)calloc(size,sizeof(int));
list->last->estado = calloc(size, sizeof *(list->last->estado));
// For consistency
// list->last-> next = calloc(1,sizeof(element));
list->last-> next = calloc(1,sizeof *(list->last-> next));