C memory properties

Discussion in 'C Programming' started by aegis, Dec 9, 2005.

  1. aegis

    aegis Guest

    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?

    --
    aegis
     
    aegis, Dec 9, 2005
    #1
    1. Advertising

  2. In article <>,
    aegis <> 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
     
    Walter Roberson, Dec 9, 2005
    #2
    1. Advertising

  3. aegis

    Malcolm Guest

    "aegis" <> 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.
     
    Malcolm, Dec 10, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nathan Sokalski
    Replies:
    0
    Views:
    919
    Nathan Sokalski
    Oct 17, 2005
  2. =?Utf-8?B?Q2hyaXN0b3BoZSBQZWlsbGV0?=

    CompositeControls: ViewState properties w/ Mapped properties probl

    =?Utf-8?B?Q2hyaXN0b3BoZSBQZWlsbGV0?=, Jan 19, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    1,141
    Steven Cheng[MSFT]
    Jan 19, 2006
  3. Kent Lichty
    Replies:
    0
    Views:
    838
    Kent Lichty
    Apr 16, 2004
  4. Victor Porton
    Replies:
    1
    Views:
    702
    Steven T. Hatton
    Aug 29, 2004
  5. Wardie
    Replies:
    4
    Views:
    490
    Wardie
    Nov 8, 2006
Loading...

Share This Page