by kerneis · 10y ago · 0 comments
Moreover, the study was explicitly not about ISO C: "We were not asking what the ISO C standard permits, which is often more restrictive, or about obsolete or obscure hardware or compilers. We focussed on the behaviour of memory and pointers. This is a step towards an unambiguous and mathematically precise definition of the de facto standards: the C dialects that are actually used by systems programmers and implemented by mainstream compilers."
Here is an actual example of a comment to this question:
I would expect this code to work:
struct foo
{
char a;
double b;
};
foo p;
foo q;
memset( &p, 0, sizeof( p ) );
memset( &q, 0, sizeof( q ) );
p.a = 1;
q.a = 1;
assert( memcmp( &p, &q, sizeof( foo ) ) == 0 );Q64. After an explicit write of zero to a padding
byte followed by a write to adjacent members of
the structure, does the padding byte hold a
well-defined zero value? (not an unspecified
value)
#include <stdio.h>
#include <stddef.h>
typedef struct { char c; float f; int i; } st;
int main() {
// check there is a padding byte between c and f
size_t offset_padding = offsetof(st,c)+sizeof(char);
if (offsetof(st,f)>offset_padding) {
st s;
unsigned char *p =
((unsigned char*)(&s)) + offset_padding;
*p = 0;
s.c = 'A';
s.f = 1.0;
s.i = 42;
unsigned char c3 = *p;
// does c3 hold 0, not an unspecified value?
printf("c3=0x%x\n",c3);
}
return 0;
}
Some of the questions have clear answers with respect to either the ISO or de facto standards, but many do not - that's the point.