当我对结构进行浅表复制时,灵活的数组成员不会被复制
Flexible array member not getting copied when I make a shallow copy of a struct
我通过以下方式对我的结构进行了浅拷贝:
struct Student{
char *name;
int age;
Courses *list; //First course (node)
Student *friends[]; //Flexible array member stores other student pointers
}Student;
void shallowCopy(const Student *one){
Student *oneCopy = malloc(sizeof(one) + 20*sizeof(Student*));
*oneCopy = *one; <--------------- ERROR POINTS TO THIS LINE
}
当我检查灵活数组成员 it oneCopy
的第一个元素时,它为空。但是如果我检查原始结构中灵活数组成员的第一个元素,它会成功打印出指针。原始结构的所有其他组件都被复制过来,如名称和链表。只有灵活的数组成员没有被复制。有谁知道我做错了什么?
Does anyone know what I am doing wrong?
正在尝试使用赋值来复制具有灵活数组成员的结构。来自标准(6.7.2.1):
The assignment *s1 = *s2
only copies the member n
[i.e. the part of the struct that isn't a flexible array]; if any of the array elements are within the first sizeof (struct s)
bytes of the structure, they might be copied or simply overwritten with indeterminate values.
基本上,当 C 编译器看到一个具有灵活数组成员的结构时,它不知道它到底有多大,所以它认为它足够大以容纳其他成员,加上 可能更多:
In particular, the size of the structure is as if the flexible array member were omitted except that it may have more trailing padding than the omission would imply.
这就是 sizeof(*one)
的大小,那是 当您执行 *oneCopy = *one;
.
时复制的内容的大小
既然你做显然知道整个结构的大小,为了malloc
它,只需使用memcpy
复制那么多字节。或者,如果您担心这在某种程度上不可移植(老实说我不确定),请执行分配,然后 使用循环将每个元素从 one->friends
复制到
oneCopy->friends
.
在您的代码片段中,结构声明是错误的。我认为你的意思是结构的 typedef 而不是声明结构的对象。
例如
typedef struct Student
^^^^^^^
{
char *name;
int age;
Courses *list; //First course (node)
struct Student *friends[]; //Flexible array memeber stores other student pointers
^^^^^^^^^^^^^^
} Student;
这个malloc的调用也是错误的
Student *oneCopy = malloc(sizeof(one) + 20*sizeof(Student*));
应该有
Student *oneCopy = malloc(sizeof( *one ) + 20*sizeof(Student*));
^^^^^
这是一个演示程序,展示了如何编写该函数
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
typedef struct Student
{
char *name;
int age;
// Courses *list; //First course (node)
struct Student *friends[]; //Flexible array memeber stores other student pointers
} Student;
Student * shallowCopy( const Student *one, size_t friends )
{
Student *oneCopy = malloc( sizeof( Student ) + friends * sizeof( Student * ) );
*oneCopy = *one;
memcpy( oneCopy->friends, one->friends, friends * sizeof( Student * ) );
return oneCopy;
}
int main( void )
{
Student *one = malloc( sizeof( Student ) + sizeof( Student * ) );
one->friends[0] = malloc( sizeof( Student ) );
one->friends[0]->age = 20;
Student *oneCopy = shallowCopy( one, 1 );
printf( "Age = %d\n", oneCopy->friends[0]->age );
free( one->friends[0] );
free( one );
free( oneCopy );
}
它的输出是
Age = 20
考虑到该结构最好还包含一个数据成员,用于存储灵活数组中的元素数量。:)
我通过以下方式对我的结构进行了浅拷贝:
struct Student{
char *name;
int age;
Courses *list; //First course (node)
Student *friends[]; //Flexible array member stores other student pointers
}Student;
void shallowCopy(const Student *one){
Student *oneCopy = malloc(sizeof(one) + 20*sizeof(Student*));
*oneCopy = *one; <--------------- ERROR POINTS TO THIS LINE
}
当我检查灵活数组成员 it oneCopy
的第一个元素时,它为空。但是如果我检查原始结构中灵活数组成员的第一个元素,它会成功打印出指针。原始结构的所有其他组件都被复制过来,如名称和链表。只有灵活的数组成员没有被复制。有谁知道我做错了什么?
Does anyone know what I am doing wrong?
正在尝试使用赋值来复制具有灵活数组成员的结构。来自标准(6.7.2.1):
The assignment
*s1 = *s2
only copies the membern
[i.e. the part of the struct that isn't a flexible array]; if any of the array elements are within the firstsizeof (struct s)
bytes of the structure, they might be copied or simply overwritten with indeterminate values.
基本上,当 C 编译器看到一个具有灵活数组成员的结构时,它不知道它到底有多大,所以它认为它足够大以容纳其他成员,加上 可能更多:
In particular, the size of the structure is as if the flexible array member were omitted except that it may have more trailing padding than the omission would imply.
这就是 sizeof(*one)
的大小,那是 当您执行 *oneCopy = *one;
.
既然你做显然知道整个结构的大小,为了malloc
它,只需使用memcpy
复制那么多字节。或者,如果您担心这在某种程度上不可移植(老实说我不确定),请执行分配,然后 使用循环将每个元素从 one->friends
复制到
oneCopy->friends
.
在您的代码片段中,结构声明是错误的。我认为你的意思是结构的 typedef 而不是声明结构的对象。
例如
typedef struct Student
^^^^^^^
{
char *name;
int age;
Courses *list; //First course (node)
struct Student *friends[]; //Flexible array memeber stores other student pointers
^^^^^^^^^^^^^^
} Student;
这个malloc的调用也是错误的
Student *oneCopy = malloc(sizeof(one) + 20*sizeof(Student*));
应该有
Student *oneCopy = malloc(sizeof( *one ) + 20*sizeof(Student*));
^^^^^
这是一个演示程序,展示了如何编写该函数
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
typedef struct Student
{
char *name;
int age;
// Courses *list; //First course (node)
struct Student *friends[]; //Flexible array memeber stores other student pointers
} Student;
Student * shallowCopy( const Student *one, size_t friends )
{
Student *oneCopy = malloc( sizeof( Student ) + friends * sizeof( Student * ) );
*oneCopy = *one;
memcpy( oneCopy->friends, one->friends, friends * sizeof( Student * ) );
return oneCopy;
}
int main( void )
{
Student *one = malloc( sizeof( Student ) + sizeof( Student * ) );
one->friends[0] = malloc( sizeof( Student ) );
one->friends[0]->age = 20;
Student *oneCopy = shallowCopy( one, 1 );
printf( "Age = %d\n", oneCopy->friends[0]->age );
free( one->friends[0] );
free( one );
free( oneCopy );
}
它的输出是
Age = 20
考虑到该结构最好还包含一个数据成员,用于存储灵活数组中的元素数量。:)