Я хотел глубоко скопировать структуру в другую структуру по причинам ...
Следуя второму ответу на Глубокое копирование структур с массивами символов в C (как скопировать массивы?), я придумал следующий пример :
#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
Из первого ответа на пустой возврат в непустой функции, является ли неопределенное поведение? Я понимаю, что это ограничение нарушение, но в результате эта непустая пустая функция C без оператора return глубоко копирует структуру ввода в структуру вывода.
Я просто оставлю это здесь, на всякий случай, если это может быть полезно кому-нибудь еще. Но есть ли здесь нечто большее, чем кажется на первый взгляд?
Я предполагаю, что компилятор является ключевым здесь: я использую gcc 7.4.0 под Cygwin x86_64 3.0.7 (0.338 / 5/3) и Windows 10. Я компилирую кратко:
> 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)
NULL
, что является еще одним случаем неопределенного поведения. Если вы хотите скопировать содержимое структуры, сначала должен быть совместимый объект для копирования, который вы не предоставили. - person Felix G   schedule 17.07.2020the_parameter
просто интерпретируется как возвращаемое значение (поэтому на самом деле ничего не было скопировано). Просто сравните адрес вашей исходной структуры со скопированной, и вы, вероятно, обнаружите, что они такие же - person Felix G   schedule 17.07.2020the_function
функция в ответе Питера? Вы его кардинально изменили. - person RobertS supports Monica Cellio   schedule 17.07.2020NULL
). Если вы хотите, чтобы эта функция копировалась в новый объект, вам нужно сначала использоватьmalloc()
для создания этого объекта (и проверить возвращаемое значение, чтобы убедиться, что это неNULL
). - person Felix G   schedule 17.07.2020*
s во втором операторе вthe_function
, все четко определено, но вы просто назначаете адрес одной структуры указателю целевой структуры. На самом деле у вас нет двух структур. У вас есть указатель, который снова ссылается на ту же структуру. Это также будет равноthe_struct_type * the_function (the_struct_type * the_parameter) { return the_parameter; }
- person RobertS supports Monica Cellio   schedule 17.07.2020Segmentation fault
происходит из-за этой*the_returned_variable = *the_parameter;
строки. Просто измените его наthe_returned_variable = the_parameter
. - person Shubham   schedule 17.07.2020