I wanted to deep copy a struct to another struct for reasons...
Following the second answer to "Deep copying structs with char arrays in C (How to copy the arrays?") I concocted the following example:
#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);
}
The result was:
Segmentation fault (core dumped)
Out of despair I commented the innards of the function. It was as if an invisible hand was guiding my actions. :)
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;
}
And, out of the blue, the result was the desired—not sure if what I expected, though:
the_member is 7777777
I verified this behavior defining more complex structs, containing arrays (but not pointers). This unexpected behavior allowed me to create also arrays of structs by adding the following code to 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] );
}
Resulting in:
the_member is 7777777
the_member is 7777777
the_member is 123456
the_member is 123456
the_member is 7777777
From the first answer to "Empty return in non-void function, is undefined behaviour?" I understand this is a constraint violation, but the result is that this non-void C empty function without return statement is deep copying the input struct to the output struct.
I am just going to leave this here, just in case it could be of use for anyone else. But is there more than meets the eye here?
I guess that the compiler is the key here: I am using gcc 7.4.0 under Cygwin x86_64 3.0.7(0.338/5/3) and Windows 10. I compile with a terse:
> 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)