By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,898 Members | 2,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,898 IT Pros & Developers. It's quick & easy.

C memory properties

P: n/a
According to:
http://groups.google.co.in/group/com...5?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?

--
aegis

Dec 9 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
In article <11**********************@g47g2000cwa.googlegroups .com>,
aegis <ae***@mad.scientist.com> wrote:
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) .
--
If you lie to the compiler, it will get its revenge. -- Henry Spencer
Dec 9 '05 #2

P: n/a

"aegis" <ae***@mad.scientist.com> wrote
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.
Dec 10 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.