Optimizing structure memory allocation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rahul

    Optimizing structure memory allocation

    How is the memory allocated for structures? I need to optimize the
    memory usage and bit fields are not doing the trick.

    Any details about the memory allocation for the structures would be a
    great help.

    PS - I already have asked this in gcc group. Please refrain from
    directing me towards other groups.
  • Richard Heathfield

    #2
    Re: Optimizing structure memory allocation

    rahul said:
    How is the memory allocated for structures?
    "A structure type describes a sequentially allocated set of member
    objects", says the Standard.

    Later, it adds: "If the objects pointed to are members of the same
    aggregate object, pointers to structure members declared later compare
    higher than pointers to members declared earlier in the structure".

    Finally, "There may also be unnamed padding at the end of a structure or
    union, as necessary to achieve the appropriate alignment were the
    structure or union to be a member of an array."

    <snip>
    PS - I already have asked this in gcc group. Please refrain from
    directing me towards other groups.
    Gladly, if you are happy to accept that the answers you get here will be
    related to what the language guarantees, rather than which particular
    choice an implementation might make where the language offers such a
    choice.

    --
    Richard Heathfield <http://www.cpax.org.uk >
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999

    Comment

    • Antoninus Twink

      #3
      Re: Optimizing structure memory allocation

      On 27 May 2008 at 12:25, rahul wrote:
      How is the memory allocated for structures? I need to optimize the
      memory usage and bit fields are not doing the trick.
      >
      PS - I already have asked this in gcc group. Please refrain from
      directing me towards other groups.
      You might want to look at gcc's packed attribute, which "specifies that
      a variable or structure field should have the smallest possible
      alignment - one byte for a variable, and one bit for a field, unless you
      specify a larger value with the aligned attribute."

      You can either pack specific fields, e.g.

      struct foo {
      char a;
      int b __attribute__(( packed));
      };

      or whole structs at once, e.g.

      struct bar {
      int a;
      char b;
      int c;
      char d;
      int e;
      } __attribute__(( packed));

      Comment

      • Keith Thompson

        #4
        Re: Optimizing structure memory allocation

        Antoninus Twink <nospam@nospam. invalidwrites:
        On 27 May 2008 at 12:25, rahul wrote:
        >How is the memory allocated for structures? I need to optimize the
        >memory usage and bit fields are not doing the trick.
        >>
        >PS - I already have asked this in gcc group. Please refrain from
        >directing me towards other groups.
        >
        You might want to look at gcc's packed attribute,
        [...]

        which is, of course, off-topic in comp.lang.c.

        rahul, you asked us not to direct you to other groups. Why?
        Questions about gcc-specific features are appropriate in gnu.gcc.help;
        they are not appropriate in comp.lang.c.

        Or you can consult the extensive documentation that comes with gcc.

        --
        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
        Nokia
        "We must do something. This is something. Therefore, we must do this."
        -- Antony Jay and Jonathan Lynn, "Yes Minister"

        Comment

        • Harald van =?UTF-8?b?RMSzaw==?=

          #5
          Re: Optimizing structure memory allocation

          On Tue, 27 May 2008 13:02:33 -0700, Keith Thompson wrote:
          Right, but it's perfectly legal for the alignment restriction to be "any
          alignment is ok". It's also legal (but silly) for the compiler to
          insert padding whether it's required for alignment or not.
          It's not silly if older systems required a particular alignment, newer
          systems don't, but the compiler still inserts padding to maintain binary
          compatibility with those older systems. It's good that implementors are
          given a lot of freedom in choosing what works best for their systems.

          Comment

          • Eric Sosman

            #6
            Re: Optimizing structure memory allocation

            Keith Thompson wrote:
            Eric Sosman <Eric.Sosman@su n.comwrites:
            >Keith Thompson wrote:
            >>[...] It's also legal (but silly) for the compiler
            >>to insert padding whether it's required for alignment or not.
            > The ambiguity lies in "requiremen t." On one popular
            >platform, for example, a `double' can be accessed at any
            >address divisible by four, but can be accessed more quickly
            >if the address is also divisible by eight. Should the
            >alignment "requiremen t" be taken as four or as eight?
            >
            Could be either. My point is that the compiler is allowed to insert,
            say, 16 bytes of padding, even if it doesn't help performance at all.
            ... and my point was about the word "silly:" Until you
            can nail down "requiremen t" you can't assess the silliness
            of the padding scheme.

            --
            Eric Sosman
            esosman@ieee-dot-org.invalid

            Comment

            • Keith Thompson

              #7
              Re: Optimizing structure memory allocation

              Eric Sosman <esosman@ieee-dot-org.invalidwrit es:
              Keith Thompson wrote:
              >Eric Sosman <Eric.Sosman@su n.comwrites:
              >>Keith Thompson wrote:
              >>>[...] It's also legal (but silly) for the compiler
              >>>to insert padding whether it's required for alignment or not.
              >> The ambiguity lies in "requiremen t." On one popular
              >>platform, for example, a `double' can be accessed at any
              >>address divisible by four, but can be accessed more quickly
              >>if the address is also divisible by eight. Should the
              >>alignment "requiremen t" be taken as four or as eight?
              >Could be either. My point is that the compiler is allowed to insert,
              >say, 16 bytes of padding, even if it doesn't help performance at all.
              >
              ... and my point was about the word "silly:" Until you
              can nail down "requiremen t" you can't assess the silliness
              of the padding scheme.
              Ok, good point.

              To re-state my point more precisely, a compiler is allowed to insert
              padding between struct members even if doing so has no benefit
              whatsoever. (I don't know of any compilers that actually do so.)

              --
              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
              Nokia
              "We must do something. This is something. Therefore, we must do this."
              -- Antony Jay and Jonathan Lynn, "Yes Minister"

              Comment

              • CBFalconer

                #8
                Re: Optimizing structure memory allocation

                rahul wrote:
                >
                How is the memory allocated for structures? I need to optimize
                the memory usage and bit fields are not doing the trick.
                >
                Any details about the memory allocation for the structures would
                be a great help.
                >
                PS - I already have asked this in gcc group. Please refrain from
                directing me towards other groups.
                If you are using gcc, then a gcc group is appropriate. This one is
                not. A compiler system can use any method of memory allocation it
                pleases, so long as it works.

                --
                [mail]: Chuck F (cbfalconer at maineline dot net)
                [page]: <http://cbfalconer.home .att.net>
                Try the download section.

                ** Posted from http://www.teranews.com **

                Comment

                • Keith Thompson

                  #9
                  Re: Optimizing structure memory allocation

                  CBFalconer <cbfalconer@yah oo.comwrites:
                  rahul wrote:
                  >How is the memory allocated for structures? I need to optimize
                  >the memory usage and bit fields are not doing the trick.
                  >>
                  >Any details about the memory allocation for the structures would
                  >be a great help.
                  >>
                  >PS - I already have asked this in gcc group. Please refrain from
                  >directing me towards other groups.
                  >
                  If you are using gcc, then a gcc group is appropriate. This one is
                  not. A compiler system can use any method of memory allocation it
                  pleases, so long as it works.
                  But the C standard imposes a number of requirements on how memory is
                  allocated for structures (as we already discussed in this thread).

                  --
                  Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                  Nokia
                  "We must do something. This is something. Therefore, we must do this."
                  -- Antony Jay and Jonathan Lynn, "Yes Minister"

                  Comment

                  Working...