4-bytes or 8-bytes alignment?

Discussion in 'C Programming' started by mrby, Nov 2, 2004.

  1. mrby

    mrby Guest

    Hi,guys,

    char s1[10] = "abcde";
    char s2[3];
    printf("%d",s1-s2);

    What should be the result on a 32-bit machine? It is 8 on my machine,
    why?

    Here is the output of "uname -a" on my machine:
    $uname -a
    SunOS virgo 5.8 Generic_108528-19 sun4u sparc SUNW,Ultra-250

    --
    mrby
    mrby, Nov 2, 2004
    #1
    1. Advertising

  2. mrby wrote:
    > Hi,guys,
    >
    > char s1[10] = "abcde";
    > char s2[3];
    > printf("%d",s1-s2);
    >


    How are these two variables's1' and 's2' related and
    why would you want to subtract them ?

    > What should be the result on a 32-bit machine? It is 8 on my machine,
    > why?


    The starting address of where s1 is allocated is entirely independent
    of where s2 is allocated and vice-versa. So it does not matter
    what your machine is. For the same machine, a different implementation
    can give a diff. answer.

    --
    Karthik. http://akktech.blogspot.com .
    ' Remove _nospamplz from my email to mail me. '
    Karthik Kumar, Nov 2, 2004
    #2
    1. Advertising

  3. mrby

    Jack Klein Guest

    On 1 Nov 2004 19:19:48 -0800, (mrby) wrote in
    comp.lang.c:

    > Hi,guys,
    >
    > char s1[10] = "abcde";
    > char s2[3];
    > printf("%d",s1-s2);


    Comparing pointers that are not part of the same object or array, or
    one past the end of the same object or array, produces undefined
    behavior.

    > What should be the result on a 32-bit machine?


    There is no 'should', your program produces undefined behavior so the
    C standard no longer places any requirements at all on what happens.

    > It is 8 on my machine,
    > why?


    Since the behavior is undefined, there is no 'why' as far as C is
    concerned.

    > Here is the output of "uname -a" on my machine:
    > $uname -a
    > SunOS virgo 5.8 Generic_108528-19 sun4u sparc SUNW,Ultra-250


    What is 'uname' and why do we care what it outputs?

    If you are questioning the alignment your compiler uses in defining
    data objects, and there is no guarantee that the output of your code
    has anything at all to do with that, then you are asking in the wrong
    place.

    Data alignment in C is not defined by the language, it is specifically
    implementation-defined. That means it is up to the compiler provider
    to do whatever it is they want to do. It may be based on the
    architecture of the underlying hardware, it may be based on the kind
    of car the head programmer drives, it may be based on the fact that
    the compiler writer has stock in a memory chip company, and wants to
    make sure that you need to buy lots of extra memory.

    If you want to know what alignment your compiler uses and why, either
    contact their technical support or ask in a group that supports your
    compiler. It is not a language issue.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Nov 2, 2004
    #3
  4. mrby

    Mike Wahler Guest

    "mrby" <> wrote in message
    news:...
    > Hi,guys,
    >
    > char s1[10] = "abcde";
    > char s2[3];
    > printf("%d",s1-s2);
    >
    > What should be the result on a 32-bit machine?


    It could be anything, or nothing at all. Attempting
    to compute the difference between two addresses which
    are not within the same object (or in the case of
    any array, one element past its end) produces undefined behavior.

    Also undefined is the your use of the %d printf() specifier
    for an object not of type 'int'.

    Also note that simply because your two array definitions
    occur sequentially in your code, doesn't mean they will
    be stored anywhere near each other in memory.

    >It is 8 on my machine,
    > why?


    Just because. Tomorrow it might be 42.

    > Here is the output of "uname -a" on my machine:
    > $uname -a
    > SunOS virgo 5.8 Generic_108528-19 sun4u sparc SUNW,Ultra-250


    Your machine doesn't matter. Here we discuss ISO standard C,
    which is platform-neutral, and whose specification deems the
    behavior of that code as undefined.

    As far as alignment requirements which might be imposed by
    your particular implementation and/or platform, you'll need
    to consult your documentation. The C language doesn't control
    that.


    -Mike
    Mike Wahler, Nov 2, 2004
    #4
  5. mrby wrote:
    > Hi,guys,
    >
    > char s1[10] = "abcde";
    > char s2[3];
    > printf("%d",s1-s2);
    >
    > What should be the result on a 32-bit machine? It is 8 on my machine,
    > why?

    Whatever the compiler thinks suitable,often influenced by hardware.

    It might very well fail somehow also, as you invoke undefined behavior
    with the above.

    On most common machines with a flat memory layout you'll
    get the diffrence in memory locations though.

    The compiler/linker might decide reorder your 2 variables...

    --
    Nils O. Selåsdal
    www.utelsystems.com
    =?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=, Nov 2, 2004
    #5
  6. mrby

    pete Guest

    Mike Wahler wrote:
    >
    > "mrby" <> wrote in message
    > news:...
    > > Hi,guys,
    > >
    > > char s1[10] = "abcde";
    > > char s2[3];
    > > printf("%d",s1-s2);
    > >
    > > What should be the result on a 32-bit machine?

    >
    > It could be anything, or nothing at all. Attempting
    > to compute the difference between two addresses which
    > are not within the same object (or in the case of
    > any array, one element past its end) produces undefined behavior.
    >
    > Also undefined is the your use of the %d printf() specifier
    > for an object not of type 'int'.


    s1-s2 is an expression of type ptrdiff_t.
    There are no objects of type ptrdiff_t,
    refered to in the above code.

    --
    pete
    pete, Nov 2, 2004
    #6
  7. pete wrote:
    > Mike Wahler wrote:
    >
    >>"mrby" <> wrote in message
    >>news:...
    >>
    >>>Hi,guys,
    >>>
    >>>char s1[10] = "abcde";
    >>>char s2[3];
    >>>printf("%d",s1-s2);
    >>>
    >>>What should be the result on a 32-bit machine?

    >>
    >>It could be anything, or nothing at all. Attempting
    >>to compute the difference between two addresses which
    >>are not within the same object (or in the case of
    >>any array, one element past its end) produces undefined behavior.
    >>
    >>Also undefined is the your use of the %d printf() specifier
    >>for an object not of type 'int'.

    >
    >
    > s1-s2 is an expression of type ptrdiff_t.
    > There are no objects of type ptrdiff_t,
    > refered to in the above code.

    Not really. Two pointers to compatible types may be subtracted
    to yield ptrdiff_t, but is only valid for two pointers into the
    same array, or one past it.

    --
    Nils O. Selåsdal
    www.utelsystems.com
    =?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=, Nov 2, 2004
    #7
  8. mrby

    Mike Wahler Guest

    "pete" <> wrote in message
    news:...
    > Mike Wahler wrote:
    > >
    > > "mrby" <> wrote in message
    > > news:...
    > > > Hi,guys,
    > > >
    > > > char s1[10] = "abcde";
    > > > char s2[3];
    > > > printf("%d",s1-s2);
    > > >
    > > > What should be the result on a 32-bit machine?

    > >
    > > It could be anything, or nothing at all. Attempting
    > > to compute the difference between two addresses which
    > > are not within the same object (or in the case of
    > > any array, one element past its end) produces undefined behavior.
    > >
    > > Also undefined is the your use of the %d printf() specifier
    > > for an object not of type 'int'.

    >
    > s1-s2 is an expression of type ptrdiff_t.
    > There are no objects of type ptrdiff_t,
    > refered to in the above code.


    OK, make that "for a value not of type int".

    All better now? :)

    -Mike
    Mike Wahler, Nov 2, 2004
    #8
  9. On 1 Nov 2004 19:19:48 -0800, in comp.lang.c , (mrby)
    wrote:

    >Hi,guys,
    >
    >char s1[10] = "abcde";
    >char s2[3];
    >printf("%d",s1-s2);
    >
    >What should be the result on a 32-bit machine?


    absolutely anything.

    >It is 8 on my machine, why?


    luck.

    To explain more fully, there's nothing that requires adjacently declared
    pointers to be stored adjacently, nor for any specific meaning to be given
    to the difference between two arbitrary pointers.

    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>


    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
    Mark McIntyre, Nov 2, 2004
    #9
    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. Jason Collins
    Replies:
    3
    Views:
    6,015
    Jason Collins
    Feb 18, 2004
  2. Replies:
    5
    Views:
    546
    Flash Gordon
    Apr 9, 2006
  3. Yandos
    Replies:
    12
    Views:
    5,118
    Pete Becker
    Sep 15, 2005
  4. Replies:
    8
    Views:
    502
    Bob Hairgrove
    Apr 10, 2006
  5. Allen
    Replies:
    6
    Views:
    9,699
    Maxim Yegorushkin
    Jan 8, 2009
Loading...

Share This Page