C memory properties

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?
 
W

Walter Roberson

aegis said:
struct xyz { int x; int y; int z; };
int *ip = &x_y_z.z;
ptrdiff_t diff = (char *)ip - (char *)xyz_ptr;
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'?

The C standard says so. It says that within structures, items are
stored at increasing addresses.

The standard does not define the order of bytes within an arithmetic
type.

The standard allows for internal padding, so the difference
you get out of the program you showed, will not necessarily be the same
as 2 * sizeof(int) .
 
M

Malcolm

aegis said:
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?
Your attitude is right, but the detail is wrong.

C does require that the address of the first element of the structure be the
same as the address of the structure, and that members be arranged in
increasing order. This is to make it easier to interface with other
languages, and to join up code which uses different structures.

However as a general rule a structure should be treated as a black box, with
the members at arbitrary locations within it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top