A
aegis
According to:
http://groups.google.co.in/group/comp.lang.c/msg/dd6a40ce090ec975?dmode=source
This is valid C code:
#include <stdio.h>
#include <stddef.h>
struct xyz { int x; int y; int z; };
int main(void)
{
struct xyz x_y_z = { 0 };
struct xyz *xyz_ptr = &x_y_z;
int *ip = &x_y_z.z;
ptrdiff_t diff = (char *)ip - (char *)xyz_ptr;
printf("%d\n", (int)diff);
return 0;
}
Yes, it is. But it is not semantically valid, right? Because as far as
I know,
this assumes a particular kind of memory model. Who is to say
that the address stored in 'ip' is a value that is /greater/ than that
stored in 'xyz_ptr'?
It could just as well be likely that 'xyz_ptr' contains a value that
could be regarded as greater than that stored in 'ip', yes?
In which case, the above invokes implementation defined behavior?
Or am I looking at subtraction on two pointer values incorrectly?
Instead of thinking about it in normal terms, regardless of
the values(such as memory addresses that may be assigned to
objects incrementally or decrementally), it should produce the proper
offset under a conforming virtual C machine?
http://groups.google.co.in/group/comp.lang.c/msg/dd6a40ce090ec975?dmode=source
This is valid C code:
#include <stdio.h>
#include <stddef.h>
struct xyz { int x; int y; int z; };
int main(void)
{
struct xyz x_y_z = { 0 };
struct xyz *xyz_ptr = &x_y_z;
int *ip = &x_y_z.z;
ptrdiff_t diff = (char *)ip - (char *)xyz_ptr;
printf("%d\n", (int)diff);
return 0;
}
Yes, it is. But it is not semantically valid, right? Because as far as
I know,
this assumes a particular kind of memory model. Who is to say
that the address stored in 'ip' is a value that is /greater/ than that
stored in 'xyz_ptr'?
It could just as well be likely that 'xyz_ptr' contains a value that
could be regarded as greater than that stored in 'ip', yes?
In which case, the above invokes implementation defined behavior?
Or am I looking at subtraction on two pointer values incorrectly?
Instead of thinking about it in normal terms, regardless of
the values(such as memory addresses that may be assigned to
objects incrementally or decrementally), it should produce the proper
offset under a conforming virtual C machine?