【发布时间】:2020-07-17 07:24:11
【问题描述】:
出于某种原因,我想将一个结构深复制到另一个结构...
根据the second answer to "Deep copying structs with char arrays in C (How to copy the arrays?"),我编造了以下示例:
#include <inttypes.h>
#include <stdlib.h>
#include <stdio.h>
#define THE_ARRAY_SIZE 10
struct the_struct
{
uint64_t the_member;
};
typedef struct the_struct the_struct_type;
the_struct_type *the_function(the_struct_type *the_parameter)
{
the_struct_type *the_returned_variable = NULL;
*the_returned_variable = *the_parameter;
return the_returned_variable;
}
void the_show( the_struct_type *the_parameter)
{
printf( "the_member is %" PRIu64 "\n", the_parameter->the_member);
}
int main(int argc, char *argv[]) {
the_struct_type the_source;
the_struct_type *the_target = NULL;
the_source.the_member = 7777777;
the_target = the_function(&the_source);
the_show(the_target);
}
结果是:
Segmentation fault (core dumped)
出于绝望,我评论了函数的内部情况。就好像一只无形的手在指导我的行动。 :)
the_struct_type *the_function(the_struct_type *the_parameter)
{
//the_struct_type *the_returned_variable = NULL;
//*the_returned_variable = *the_parameter;
//return the_returned_variable;
}
而且,出乎意料的是,结果是我们想要的——但不确定是否符合我的预期:
the_member is 7777777
我验证了这种行为,它定义了更复杂的结构,包含数组(但不包含指针)。这种意外行为允许我通过将以下代码添加到 main 来创建结构数组:
int main (int argc, char *argv[]) {
/* */
the_struct_type *the_array_of_struct_type = calloc(THE_ARRAY_SIZE, sizeof *the_array_of_struct_type);
the_array_of_struct_type[0] = *the_target;
the_show( &the_array_of_struct_type[0] );
the_source.the_member = 123456;
the_target = the_function( &the_source );
the_show(the_target);
the_array_of_struct_type[1] = *the_target;
the_show( &the_array_of_struct_type[1] );
the_show( &the_array_of_struct_type[0] );
}
导致:
the_member is 7777777
the_member is 7777777
the_member is 123456
the_member is 123456
the_member is 7777777
来自the first answer to "Empty return in non-void function, is undefined behaviour?" 我了解到这是一个违反约束,但结果是这个没有返回语句的非void C 空函数将输入结构深复制到输出结构。
我将把它留在这里,以防万一它对其他人有用。但是,这里还有更多的东西吗?
我猜编译器是这里的关键:我在 Cygwin x86_64 3.0.7(0.338/5/3) 和 Windows 10 下使用 gcc 7.4.0。我用简洁的方式编译:
> gcc code.c -o executable.exe
> gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/lto-wrapper.exe
Target: x86_64-pc-cygwin
Configured with: /cygdrive/i/szsz/tmpp/gcc/gcc-7.4.0-1.x86_64/src/gcc-7.4.0/configure --srcdir=/cygdrive/i/szsz/tmpp/gcc/gcc-7.4.0-1.x86_64/src/gcc-7.4.0 --prefix=/usr --exec-prefix=/usr --localstatedir=/var --sysconfdir=/etc --docdir=/usr/share/doc/gcc --htmldir=/usr/share/doc/gcc/html -C --build=x86_64-pc-cygwin --host=x86_64-pc-cygwin --target=x86_64-pc-cygwin --without-libiconv-prefix --without-libintl-prefix --libexecdir=/usr/lib --enable-shared --enable-shared-libgcc --enable-static --enable-version-specific-runtime-libs --enable-bootstrap --enable-__cxa_atexit --with-dwarf2 --with-tune=generic --enable-languages=ada,c,c++,fortran,lto,objc,obj-c++ --enable-graphite --enable-threads=posix --enable-libatomic --enable-libcilkrts --enable-libgomp --enable-libitm --enable-libquadmath --enable-libquadmath-support --disable-libssp --enable-libada --disable-symvers --with-gnu-ld --with-gnu-as --with-cloog-include=/usr/include/cloog-isl --without-libiconv-prefix --without-libintl-prefix --with-system-zlib --enable-linker-build-id --with-default-libstdcxx-abi=gcc4-compatible --enable-libstdcxx-filesystem-ts
Thread model: posix
gcc version 7.4.0 (GCC)
【问题讨论】:
-
使用一个非 void 函数的返回值,该函数已经落到最后(即没有执行 return 语句)确实是未定义的行为。在您的情况下可能发生的情况是
the_parameter只是被解释为返回值(因此实际上没有复制任何内容)。只需将原始结构的地址与“复制”的地址进行比较,您可能会发现它们是相同的 -
@DIVVS IVLIVS
the_function与彼得回答中的函数有什么关系?你彻底改变了它。 -
@DIVVSIVLIVS 我想我能理解你的意图,但这并不是真正的“复制”。当您在
the_function的第二个语句中省略*s 时,一切都已明确定义,但您只需将一个结构的地址分配给目标结构指针。你实际上没有两个结构。您有一个指针再次引用相同的结构。这也将等于the_struct_type * the_function (the_struct_type * the_parameter) { return the_parameter; } -
@DIVVSIVLIVS 对我来说,您在问什么有点不清楚,因为标题提出了一个问题,而问题正文包含许多不相关的内容。您的问题是如标题所暗示的,还是您真的在问应该如何正确实施深拷贝?
-
@DIVVSIVLIVS ... 以及进一步:您写的是深拷贝,但代码中没有深拷贝
标签: c gcc struct cygwin deep-copy