How does malloc align pointer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • junky_fellow@yahoo.co.in

    #1

    How does malloc align pointer

    The standard says that,
    The pointer returned by malloc(), if the allocation
    succeeds is suitably aligned so that it may be assigned to a
    pointer to any type of object.

    Since, the alignment cannot be done portably, is that mean
    that malloc is implementation specific ? The malloc()
    written for one implementation may not be work on other
    implementations ?

    Also, how does malloc() determine, which pointer type has the most
    strict alignment storage restrictions ? Because it has to return
    a pointer that would always be aligned to the pointer type that
    has most strict alignment storage restriction.

  • Keith Thompson

    #2
    Re: How does malloc align pointer

    junky_fellow@ya hoo.co.in writes:[color=blue]
    > The standard says that,
    > The pointer returned by malloc(), if the allocation
    > succeeds is suitably aligned so that it may be assigned to a
    > pointer to any type of object.[/color]

    Right.
    [color=blue]
    > Since, the alignment cannot be done portably, is that mean
    > that malloc is implementation specific ? The malloc()
    > written for one implementation may not be work on other
    > implementations ?[/color]

    Exactly.
    [color=blue]
    > Also, how does malloc() determine, which pointer type has the most
    > strict alignment storage restrictions ? Because it has to return
    > a pointer that would always be aligned to the pointer type that
    > has most strict alignment storage restriction.[/color]

    The person who writes the malloc() function has to know enough about
    the compiler to know what alignment is required.

    --
    Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.

    Comment

    • Chris Dollin

      #3
      Re: How does malloc align pointer

      junky_fellow@ya hoo.co.in wrote:
      [color=blue]
      > The standard says that,
      > The pointer returned by malloc(), if the allocation
      > succeeds is suitably aligned so that it may be assigned to a
      > pointer to any type of object.
      >
      > Since, the alignment cannot be done portably, is that mean
      > that malloc is implementation specific ?[/color]

      Yes. (Which is one reason it's in the C library.)
      [color=blue]
      > The malloc()
      > written for one implementation may not be work on other
      > implementations ?[/color]

      That's right.
      [color=blue]
      > Also, how does malloc() determine, which pointer type has the most
      > strict alignment storage restrictions ? Because it has to return
      > a pointer that would always be aligned to the pointer type that
      > has most strict alignment storage restriction.[/color]

      The chap who wrote the malloc knows the implementation they're
      writing it for, and hence knows what the pointer alignments
      are, and hence which one is the strictest.

      --
      Chris "electric hedgehog" Dollin
      It's called *extreme* programming, not *stupid* programming.

      Comment

      • Anonymous 7843

        #4
        Re: How does malloc align pointer

        In article <1119336839.446 568.31940@f14g2 000cwb.googlegr oups.com>,
        <junky_fellow@y ahoo.co.in> wrote:[color=blue]
        >
        >Also, how does malloc() determine, which pointer type has the most
        >strict alignment storage restrictions ? Because it has to return
        >a pointer that would always be aligned to the pointer type that
        >has most strict alignment storage restriction.[/color]

        Alignment is dictated by properties of the host CPU and operating
        system architecture, which seldom change. Some malloc's just
        have a macro or constant set to 4, 8, or 16 (for example) and
        it just stays that way for decades.

        A malloc meant for more portable use (like the one in the GNU C
        library) probably has compile time tests or flags in the
        makefile that help determine what CPU and architecture are
        in play, combined with some checks on sizeof for some
        basic types (long, double).

        The final determination of alignment can take into
        account the requested size of the item that is passed to
        malloc. A request for two bytes of storage might only be
        aligned on a two-byte boundary, on the valid assumption
        that even though the CPU might require 4 or 8 byte alignment
        in the general case, the only things that fit into a 2 byte
        space (char, short) happen to require only 2 byte alignment.
        --
        7842++

        Comment

        • Lawrence Kirby

          #5
          Re: How does malloc align pointer

          On Tue, 21 Jun 2005 17:59:43 +0000, Anonymous 7843 wrote:

          ....
          [color=blue]
          > The final determination of alignment can take into
          > account the requested size of the item that is passed to
          > malloc. A request for two bytes of storage might only be
          > aligned on a two-byte boundary, on the valid assumption
          > that even though the CPU might require 4 or 8 byte alignment
          > in the general case, the only things that fit into a 2 byte
          > space (char, short) happen to require only 2 byte alignment.[/color]

          The standard requires a non-null return from malloc() to be appropriately
          aligned for any type, irrespective of the size of the allocation. It isn't
          clear that a strictly conforming program could tell the difference in
          every case but an implementation that doesn't stick to this rule is
          treading on uncertain ground.

          Lawrence

          Comment

          • Anonymous 7843

            #6
            Re: How does malloc align pointer

            In article <pan.2005.06.21 .23.33.16.26600 0@netactive.co. uk>,
            Lawrence Kirby <lknews@netacti ve.co.uk> wrote:[color=blue]
            >
            >
            >On Tue, 21 Jun 2005 17:59:43 +0000, Anonymous 7843 wrote:
            >
            >...
            >[color=green]
            >> The final determination of alignment can take into
            >> account the requested size of the item that is passed to
            >> malloc. A request for two bytes of storage might only be
            >> aligned on a two-byte boundary, on the valid assumption
            >> that even though the CPU might require 4 or 8 byte alignment
            >> in the general case, the only things that fit into a 2 byte
            >> space (char, short) happen to require only 2 byte alignment.[/color]
            >
            >The standard requires a non-null return from malloc() to be appropriately
            >aligned for any type, irrespective of the size of the allocation. It isn't
            >clear that a strictly conforming program could tell the difference in
            >every case but an implementation that doesn't stick to this rule is
            >treading on uncertain ground.
            >
            >Lawrence[/color]

            A pointer-type-mismatch-signalling implementation might want to
            steer clear of this trick. Can't think of anything else.
            --
            7842++

            Comment

            • Lawrence Kirby

              #7
              Re: How does malloc align pointer

              On Tue, 21 Jun 2005 23:24:21 +0000, Anonymous 7843 wrote:
              [color=blue]
              > In article <pan.2005.06.21 .23.33.16.26600 0@netactive.co. uk>,
              > Lawrence Kirby <lknews@netacti ve.co.uk> wrote:[color=green]
              >>
              >>
              >>On Tue, 21 Jun 2005 17:59:43 +0000, Anonymous 7843 wrote:
              >>
              >>...
              >>[color=darkred]
              >>> The final determination of alignment can take into
              >>> account the requested size of the item that is passed to
              >>> malloc. A request for two bytes of storage might only be
              >>> aligned on a two-byte boundary, on the valid assumption
              >>> that even though the CPU might require 4 or 8 byte alignment
              >>> in the general case, the only things that fit into a 2 byte
              >>> space (char, short) happen to require only 2 byte alignment.[/color]
              >>
              >>The standard requires a non-null return from malloc() to be appropriately
              >>aligned for any type, irrespective of the size of the allocation. It isn't
              >>clear that a strictly conforming program could tell the difference in
              >>every case but an implementation that doesn't stick to this rule is
              >>treading on uncertain ground.
              >>
              >>Lawrence[/color]
              >
              > A pointer-type-mismatch-signalling implementation might want to
              > steer clear of this trick. Can't think of anything else.[/color]

              An issue would be something like this

              char *pc = malloc(1);

              if (pc != NULL)
              pc = (char *)(long *)pc;

              This could fail if the return value of malloc() is not appropriately
              aligned for long. The question is that since it *is* required to be so
              aligned whether the cast to long * and back is valid.

              Lawrence




              Comment

              • Anonymous 7843

                #8
                Re: How does malloc align pointer

                In article <pan.2005.06.22 .11.23.53.48500 0@netactive.co. uk>,
                Lawrence Kirby <lknews@netacti ve.co.uk> wrote:[color=blue]
                >On Tue, 21 Jun 2005 23:24:21 +0000, Anonymous 7843 wrote:
                >[color=green]
                >> In article <pan.2005.06.21 .23.33.16.26600 0@netactive.co. uk>,
                >> Lawrence Kirby <lknews@netacti ve.co.uk> wrote:[color=darkred]
                >>>
                >>>The standard requires a non-null return from malloc() to be appropriately
                >>>aligned for any type, irrespective of the size of the allocation. It isn't
                >>>clear that a strictly conforming program could tell the difference in
                >>>every case but an implementation that doesn't stick to this rule is
                >>>treading on uncertain ground.
                >>>
                >>>Lawrence[/color]
                >>
                >> A pointer-type-mismatch-signalling implementation might want to
                >> steer clear of this trick. Can't think of anything else.[/color]
                >
                >An issue would be something like this
                >
                > char *pc = malloc(1);
                >
                > if (pc != NULL)
                > pc = (char *)(long *)pc;
                >
                >This could fail if the return value of malloc() is not appropriately
                >aligned for long. The question is that since it *is* required to be so
                >aligned whether the cast to long * and back is valid.[/color]

                I don't see any question about it. The cast is valid C.

                The implementor must be aware of whether the pointers are compatible or
                not _on that implementation_ and make the choice to use the sub-aligned-
                malloc trick accordingly. He may have other reasons for not using it
                (i.e. it's an untested wild suggestion from some anonymous borderline
                crackpot on comp.lang.c).

                One subtlety is that on a lot of ostensibly pointer-alignment-sensitive
                architectures (SPARC, for one) the pointers are not really checked for
                alignment until dereferencing, in other words the ptr alignment stuff
                is a misnomer: it would be better to call it dereference alignment or
                object alignment.
                --
                7842++

                Comment

                • Peter Ammon

                  #9
                  Re: How does malloc align pointer

                  Keith Thompson wrote:[color=blue]
                  > junky_fellow@ya hoo.co.in writes:[/color]
                  [...][color=blue]
                  >[color=green]
                  >>Also, how does malloc() determine, which pointer type has the most
                  >>strict alignment storage restrictions ? Because it has to return
                  >>a pointer that would always be aligned to the pointer type that
                  >>has most strict alignment storage restriction.[/color]
                  >
                  >
                  > The person who writes the malloc() function has to know enough about
                  > the compiler to know what alignment is required.[/color]

                  Is the usual union alignment hack^H^H^H^Htri ck not good enough?

                  -Peter

                  --
                  Pull out a splinter to reply.

                  Comment

                  • Keith Thompson

                    #10
                    Re: How does malloc align pointer

                    Peter Ammon <gershwin@splin termac.com> writes:[color=blue]
                    > Keith Thompson wrote:[color=green]
                    >> junky_fellow@ya hoo.co.in writes:[/color]
                    > [...][color=green]
                    >>[color=darkred]
                    >>>Also, how does malloc() determine, which pointer type has the most
                    >>>strict alignment storage restrictions ? Because it has to return
                    >>>a pointer that would always be aligned to the pointer type that
                    >>>has most strict alignment storage restriction.[/color]
                    >> The person who writes the malloc() function has to know enough about
                    >> the compiler to know what alignment is required.[/color]
                    >
                    > Is the usual union alignment hack^H^H^H^Htri ck not good enough?[/color]

                    It's not easy, and it may not even be possible, to pick a set of types
                    that's guaranteed to include one with the implementation' s strictest
                    alignment.

                    --
                    Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                    We must do something. This is something. Therefore, we must do this.

                    Comment

                    Working...