Simple allocation of two buffer on AMD64

Discussion in 'C Programming' started by jimenezrick@gmail.com, Nov 1, 2007.

  1. Guest

    - Code compiled by GCC:

    int main(void)
    {
    char a[] = "123";
    char b[] = "abc";

    printf("%p %p\n", a, b);

    return 0;
    }

    - Running it on AMD64 gives me:

    0x7fff929c9410 0x7fff929c9400

    - What is the reason for 16 bytes of distance between two address and
    not just 8 bytes?
    , Nov 1, 2007
    #1
    1. Advertising

  2. Eric Sosman Guest

    wrote:
    > - Code compiled by GCC:
    >
    > int main(void)
    > {
    > char a[] = "123";
    > char b[] = "abc";
    >
    > printf("%p %p\n", a, b);
    >
    > return 0;
    > }
    >
    > - Running it on AMD64 gives me:
    >
    > 0x7fff929c9410 0x7fff929c9400
    >
    > - What is the reason for 16 bytes of distance between two address and
    > not just 8 bytes?


    Ask the gcc implementors. It was they who decided how
    the compiler should allocate space, not the C language itself.
    C does not require a sixteen-byte separation, but does not
    forbid it.

    --
    Eric Sosman
    lid
    Eric Sosman, Nov 1, 2007
    #2
    1. Advertising

  3. santosh Guest

    wrote:

    > - Code compiled by GCC:
    >
    > int main(void)
    > {
    > char a[] = "123";
    > char b[] = "abc";
    >
    > printf("%p %p\n", a, b);


    Cast 'a' and 'b' to void *.

    >
    > return 0;
    > }
    >
    > - Running it on AMD64 gives me:
    >
    > 0x7fff929c9410 0x7fff929c9400
    >
    > - What is the reason for 16 bytes of distance between two address and
    > not just 8 bytes?


    It's an implementation detail that Standard C says nothing about. It may
    be due to alignment reasons.
    santosh, Nov 1, 2007
    #3
  4. Guest

    > It's an implementation detail that Standard C says nothing about. It may
    > be due to alignment reasons.


    I know. Just curiosity about the election of 16 bytes over 8 (what I
    expected).

    Thanks anyway.
    , Nov 1, 2007
    #4
  5. On Nov 1, 7:54 am, santosh <> wrote:
    > wrote:
    > > char a[] = "123";
    > > char b[] = "abc";

    >
    > > printf("%p %p\n", a, b);

    >
    > Cast 'a' and 'b' to void *.


    Correct me if I'm wrong, but isn't this technically a case where it
    shouldn't matter, seeing as char * and void * have the exact same
    representation?
    Justin Spahr-Summers, Nov 1, 2007
    #5
  6. In article <>,
    Justin Spahr-Summers <> wrote:

    >> > char a[] = "123";
    >> > char b[] = "abc";

    >>
    >> > printf("%p %p\n", a, b);


    >> Cast 'a' and 'b' to void *.


    >Correct me if I'm wrong, but isn't this technically a case where it
    >shouldn't matter, seeing as char * and void * have the exact same
    >representation?


    We discussed this a few weeks ago. The idea seems to be that although
    they have the same representation, they aren't necessarily passed to
    variadic functions in the same way.

    -- Richard



    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
    Richard Tobin, Nov 1, 2007
    #6
  7. Guest

    > > Cast 'a' and 'b' to void *.

    Same result.



    > Correct me if I'm wrong, but isn't this technically a case where it
    > shouldn't matter, seeing as char * and void * have the exact same
    > representation?


    Yes, in this case is similar.
    , Nov 1, 2007
    #7
  8. CBFalconer Guest

    wrote:
    >
    > - Code compiled by GCC:
    >
    > int main(void) {
    > char a[] = "123";
    > char b[] = "abc";
    >
    > printf("%p %p\n", a, b);
    > return 0;
    > }
    >
    > - Running it on AMD64 gives me:
    >
    > 0x7fff929c9410 0x7fff929c9400
    >
    > - What is the reason for 16 bytes of distance between two address
    > and not just 8 bytes?


    Many systems access memory in 128 bit chunks. Aligning objects to
    this value prevents having to do two memory accesses, when one
    suffices. Check the gcc docs for means of avoiding this if
    desired.

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Nov 1, 2007
    #8
  9. wrote:
    > - Code compiled by GCC:
    >
    > int main(void)
    > {
    > char a[] = "123";
    > char b[] = "abc";
    >
    > printf("%p %p\n", a, b);
    >
    > return 0;
    > }
    >
    > - Running it on AMD64 gives me:
    >
    > 0x7fff929c9410 0x7fff929c9400
    >
    > - What is the reason for 16 bytes of distance between two address and
    > not just 8 bytes?


    That is, of course, an issue between your hardware, your OS, and the
    version of gcc you are using. There is no reason, as far as C is
    concerned, for the addresses of two distinct objects to have any
    particular relationship. But your post is extremely amusing. The fact
    that you think it somehow appropriate for the difference to be eight
    when sizeof a and sizeof b are each four is hilarious.

    You really should include <stdio.h> when using the variadic function printf.
    Even with the special relationship between the form of (void *) and
    (char *), you should cast pointers to (void *) when using the "%p"
    specifier, since that is what it signals.
    Martin Ambuhl, Nov 1, 2007
    #9
  10. CBFalconer Guest

    jacob navia wrote:
    >

    .... snip ...

    Date:
    Fri, 02 Nov 2007 00:43:31 +0100

    Your clock is mis-set.

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>


    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Nov 1, 2007
    #10
  11. Doug Guest

    On Nov 1, 4:28 pm, Martin Ambuhl <> wrote:
    > wrote:
    > > - Code compiled by GCC:

    >
    > > int main(void)
    > > {
    > > char a[] = "123";
    > > char b[] = "abc";

    >
    > > printf("%p %p\n", a, b);

    >
    > > return 0;
    > > }

    >
    > > - Running it on AMD64 gives me:

    >


    <snip>

    > But your post is extremely amusing. The fact
    > that you think it somehow appropriate for the difference to be eight
    > when sizeof a and sizeof b are each four is hilarious.


    You don't get out enough.

    Aside from that, it would have probably been more helpful to explain
    why, rather than just laughing.

    To the OP, the definition of char 'a[] = "123;"' causes the compiler
    to allocate space for exactly 4 characters (pedant point: or more, but
    only 4 are 'legally' accessible by the code). Using sizeof on an
    array is one of those cases where the array name doesn't degenerate to
    a pointer to its first element - so 'sizeof a' will return 4
    (characters) not sizeof (char *). (I think you probably expected 8
    because the size of a pointer on your system is 8, and you expected
    the compiler to emit two pointers in your stack frame.)

    Of course, this isn't actually that relevant to your question - as
    others have pointed out, the standard says nothing about how the
    compiler has to layout locals in a stack frame (or even that it has to
    use a stack), and Jacob has probably nailed why it ends up being 16
    with your platform.

    Doug
    Doug, Nov 1, 2007
    #11
  12. Richard Guest

    CBFalconer <> writes:

    > jacob navia wrote:
    >>

    > ... snip ...
    >
    > Date:
    > Fri, 02 Nov 2007 00:43:31 +0100
    >
    > Your clock is mis-set.
    >
    > --
    > Chuck F (cbfalconer at maineline dot net)
    > Available for consulting/temporary embedded and systems.
    > <http://cbfalconer.home.att.net>


    And you are posting with two signatures. Still.
    Richard, Nov 1, 2007
    #12
  13. Doug wrote:
    > On Nov 1, 4:28 pm, Martin Ambuhl <> wrote:
    >> wrote:
    >>> - Code compiled by GCC:
    >>> int main(void)
    >>> {
    >>> char a[] = "123";
    >>> char b[] = "abc";
    >>> printf("%p %p\n", a, b);
    >>> return 0;
    >>> }
    >>> - Running it on AMD64 gives me:

    >
    > <snip>
    >
    >> But your post is extremely amusing. The fact
    >> that you think it somehow appropriate for the difference to be eight
    >> when sizeof a and sizeof b are each four is hilarious.

    >
    > You don't get out enough.


    That is uncalled for, irrelevant, and stupidly rude.
    >
    > Aside from that, it would have probably been more helpful to explain
    > why, rather than just laughing.


    If you had read what you snipped, you would know better than that. If
    you snip away explanations, of course they disappear. From the
    standpoint of the C programming language, everything you wrote after
    this line is off-topic and superfluous. As was your childish crap above.
    Martin Ambuhl, Nov 1, 2007
    #13
  14. "Richard" <> wrote in message
    news:...
    > CBFalconer <> writes:
    >> jacob navia wrote:
    >>>

    >> ... snip ...
    >>
    >> Date:
    >> Fri, 02 Nov 2007 00:43:31 +0100
    >>
    >> Your clock is mis-set.
    >>
    >> --
    >> Chuck F (cbfalconer at maineline dot net)
    >> Available for consulting/temporary embedded and systems.
    >> <http://cbfalconer.home.att.net>

    >
    > And you are posting with two signatures. Still.


    Like me, the post leaves his machine with a single signature. The second
    one is added by our news server. If your client were designed correctly, it
    would strip off both signatures when replying since the first, not last,
    occurrence of "-- " denotes the start of the signature block. Technically,
    it's all one overlong signature with a spurious "-- " in the middle.

    S

    --
    Stephen Sprunk "God does not play dice." --Albert Einstein
    CCIE #3723 "God is an inveterate gambler, and He throws the
    K5SSS dice at every possible opportunity." --Stephen Hawking


    --
    Posted via a free Usenet account from http://www.teranews.com
    Stephen Sprunk, Nov 1, 2007
    #14
  15. jacob navia Guest

    wrote:
    > - Code compiled by GCC:
    >
    > int main(void)
    > {
    > char a[] = "123";
    > char b[] = "abc";
    >
    > printf("%p %p\n", a, b);
    >
    > return 0;
    > }
    >
    > - Running it on AMD64 gives me:
    >
    > 0x7fff929c9410 0x7fff929c9400
    >
    > - What is the reason for 16 bytes of distance between two address and
    > not just 8 bytes?
    >


    The stack MUST be aligned to an 8 byte boundary in
    64 bits AMD/Intel. If that fails, each access provokes
    a misaligned read, what makes performance take a
    big hit.

    Access to XMM registers in most cases MUST be aligned to a
    16 byte boundary. If not, you can't load the 16 byte
    XMM registers quickly.

    Some compilers decide that performance is better than
    wasting memory in the alignment, and align all variables
    in the stack to a 16 byte boundary even when it is not
    needed, as in your example.

    Other compilers will say that performance by loading the XMM
    registers is lost when the cache hits go down because the
    program uses more memory and will NOT align to a 16 byte
    boundary but only to a 8 byte boundary what is bad enough.



    --
    jacob navia
    jacob at jacob point remcomp point fr
    logiciels/informatique
    http://www.cs.virginia.edu/~lcc-win32
    jacob navia, Nov 1, 2007
    #15
  16. Jack Klein Guest

    On Thu, 01 Nov 2007 14:28:08 -0000, Justin Spahr-Summers
    <> wrote in comp.lang.c:

    > On Nov 1, 7:54 am, santosh <> wrote:
    > > wrote:
    > > > char a[] = "123";
    > > > char b[] = "abc";

    > >
    > > > printf("%p %p\n", a, b);

    > >
    > > Cast 'a' and 'b' to void *.

    >
    > Correct me if I'm wrong, but isn't this technically a case where it
    > shouldn't matter, seeing as char * and void * have the exact same
    > representation?


    Yes and no. It doesn't matter, they are compatible in this case, but
    not because they have the same representation.

    Pointer to void and pointer to any of the three character types are
    required to have the same size and representation, but nowhere does
    the C standard that they be passed to functions in the same manner, in
    the absence of a prototype.

    The relevant definition on representation is 6.2.5 p26, first
    sentence:

    "A pointer to void shall have the same representation and alignment
    requirements as a pointer to a character type.39)"

    Where the "39)" is a reference to footnote 39, on the same page:

    "39) The same representation and alignment requirements are meant to
    imply interchangeability as arguments to functions, return values from
    functions, and members of unions."

    My reading of this is that implementations are strongly urged, but not
    required, to make them compatible in all of these situations. It is a
    quality of implementation whether they are or not.

    But in the particular case of an argument to *printf() to match a "%p"
    conversion specifier, compatibility is guaranteed by specific wording
    in the standard by 6.5.2.2 p6.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Nov 2, 2007
    #16
  17. Richard Guest

    "Stephen Sprunk" <> writes:

    > "Richard" <> wrote in message
    > news:...
    >> CBFalconer <> writes:
    >>> jacob navia wrote:
    >>>>
    >>> ... snip ...
    >>>
    >>> Date:
    >>> Fri, 02 Nov 2007 00:43:31 +0100
    >>>
    >>> Your clock is mis-set.
    >>>
    >>> --
    >>> Chuck F (cbfalconer at maineline dot net)
    >>> Available for consulting/temporary embedded and systems.
    >>> <http://cbfalconer.home.att.net>

    >>
    >> And you are posting with two signatures. Still.

    >
    > Like me, the post leaves his machine with a single signature. The
    > second one is added by our news server. If your client were designed


    Which is why you should either leave off the second signature OR move to
    another news service which does not attach another one.

    > correctly, it would strip off both signatures when replying since the
    > first, not last, occurrence of "-- " denotes the start of the
    > signature block. Technically, it's all one overlong signature with a
    > spurious "-- " in the middle.
    >


    Incorrect from what i can gather. The proper way is to clip the
    last. otherwise it is possible that embedded text could be cut.

    Both gnus and slrn suffer and I can't be bothered to manually snip since
    he, and now you, are the ONLY two people who feel that posting with two
    signatures is in anyway ok or justified.

    It's just a question of common decency and trying to keep usenet a civil
    place. Normally I wouldn't worry too much but since 4 out of 5 of
    Falconer's posts criticise the way other people post it is an issue. As
    someone said - he is a hypocrite.

    > S
    >
    > --
    > Stephen Sprunk "God does not play dice." --Albert Einstein
    > CCIE #3723 "God is an inveterate gambler, and He throws the
    > K5SSS dice at every possible opportunity." --Stephen Hawking
    Richard, Nov 2, 2007
    #17
  18. santosh Guest

    Stephen Sprunk wrote:

    > "Richard" <> wrote in message
    > news:...
    >> CBFalconer <> writes:
    >>> jacob navia wrote:
    >>>>
    >>> ... snip ...
    >>>
    >>> Date:
    >>> Fri, 02 Nov 2007 00:43:31 +0100
    >>>
    >>> Your clock is mis-set.
    >>>
    >>> --
    >>> Chuck F (cbfalconer at maineline dot net)
    >>> Available for consulting/temporary embedded and systems.
    >>> <http://cbfalconer.home.att.net>

    >>
    >> And you are posting with two signatures. Still.

    >
    > Like me, the post leaves his machine with a single signature. The
    > second
    > one is added by our news server. If your client were designed
    > correctly, it would strip off both signatures when replying


    As does KNode for example.

    > since the first, not last,
    > occurrence of "-- " denotes the start of the signature block.
    > Technically, it's all one overlong signature with a spurious "-- " in
    > the middle.


    I agree.
    santosh, Nov 2, 2007
    #18
  19. In article <>,
    Jack Klein <> wrote:

    >"39) The same representation and alignment requirements are meant to
    >imply interchangeability as arguments to functions, return values from
    >functions, and members of unions."
    >
    >My reading of this is that implementations are strongly urged, but not
    >required, to make them compatible in all of these situations. It is a
    >quality of implementation whether they are or not.


    My reading is that they had not considered the possibility of
    different argument passing conventions when they wrote that text. If
    they had, they should have been explicit about it.

    -- Richard
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
    Richard Tobin, Nov 2, 2007
    #19
  20. Richard Bos Guest

    "Stephen Sprunk" <> wrote:

    > "Richard" <> wrote in message
    > news:...
    > > CBFalconer <> writes:
    > >> --
    > >> Chuck F (cbfalconer at maineline dot net)
    > >> Available for consulting/temporary embedded and systems.
    > >> <http://cbfalconer.home.att.net>

    > >
    > > And you are posting with two signatures. Still.

    >
    > Like me, the post leaves his machine with a single signature. The second
    > one is added by our news server.


    And like for him, that is a piss-poor excuse for you. You _know_ your
    server does that. You should take it into account when writing your
    posts.

    Richard
    Richard Bos, Nov 2, 2007
    #20
    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. Ken
    Replies:
    24
    Views:
    3,857
    Ben Bacarisse
    Nov 30, 2006
  2. chris
    Replies:
    6
    Views:
    985
    chris
    Oct 28, 2005
  3. He Shiming
    Replies:
    0
    Views:
    289
    He Shiming
    Aug 1, 2006
  4. castironpi

    dynamic allocation file buffer

    castironpi, Sep 9, 2008, in forum: Python
    Replies:
    26
    Views:
    805
    Francesc
    Sep 16, 2008
  5. Bjarke Hammersholt Roune
    Replies:
    14
    Views:
    1,181
    Bjarke Hammersholt Roune
    Mar 6, 2011
Loading...

Share This Page