On 2005-06-10 14:02, Ruslan Ermilov <ru_at_freebsd.org> wrote: >On Fri, Jun 10, 2005 at 01:28:13PM +0300, Giorgos Keramidas wrote: >> That would be very hard to guarantee if two different modules that use >> the types are compiled with different alignment options, right? >> >> /* header1.h */ >> struct t1 {short t1s; int t1a;}; >> >> /* header2.h */ >> struct t2 {short t2s; int t2a;}; >> >> /* module1.c */ >> #include "header1.h" >> struct t1 x; >> >> /* module2.c */ >> #include "header1.h" >> #include "header2.h" >> extern struct t1 x; >> struct t2 y; >> >> If the two modules are compiled with different options that may affect >> struct member alignment, how would one ensure that it is correct to >> use code like this in module.c? >> >> y.t2s = 10; >> y.t2a = 100; >> memcpy(&x, &y, sizeof(x)); >> >> Even the use of sizeof(x) is tricky here, since there is no guarantee >> that sizeof(x) < sizeof(y). > > And if both modules use the same t1 but compiled with different alignment > options, the memcpy() in module.c won't work either, even a simple assignment > could break. :-) Probably no. Two struct t1 objects have the same type and assignment between the two may be implemented as "element-at-a-time memcpy)", as mentioned in $6.2.6.1(6) and footnote (42) of the C99 standard. This may or may not break though, but I couldn't find any good reference to it.Received on Fri Jun 10 2005 - 09:53:46 UTC
This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:38:36 UTC