Optimizing structure memory allocation - Unix

This is a discussion on Optimizing structure memory allocation - Unix ; 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....

+ Reply to Thread
Results 1 to 6 of 6

Thread: Optimizing structure memory allocation

  1. 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.

  2. Re: Optimizing structure memory allocation

    On May 27, 8:27 am, 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.


    More context would be a great help too.
    Besides padding and memory alignment:

    http://en.wikipedia.org/wiki/Data_structure_alignment

    There isn't much I can think of without more context (and even better,
    code).

    -- paulo

  3. Re: Optimizing structure memory allocation

    On May 27, 5:27 am, 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.


    The best reference will be the C standard ... try getting a copy of
    that as well if you want to know what is guaranteed by the language
    and what is system dependent in this regard.

    Ivan Novick

  4. Re: Optimizing structure memory allocation

    In article
    <7e91d38b-4728-44a7-ba17-b10a14816ecb@t12g2000prg.googlegroups.com>,
    ivan@novickmail.com wrote:

    > On May 27, 5:27 am, 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.

    >
    > The best reference will be the C standard ... try getting a copy of
    > that as well if you want to know what is guaranteed by the language
    > and what is system dependent in this regard.


    I think the C specification leaves most of this
    implementation-dependent, so it's not going to be much help for him.

    --
    Barry Margolin, barmar@alum.mit.edu
    Arlington, MA
    *** PLEASE post questions in newsgroups, not directly to me ***
    *** PLEASE don't copy me on replies, I'll read them in the group ***

  5. Re: 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.


    Structure members are allocated *in order*. Any member may have
    padding after it (thus, there may be no padding before the first
    member). That's it for what the C Standard guarantees. Padding
    is *internal*, that is, it counts as part of sizeof(struct whatever).

    Although not guaranteed by the C Standard, usually padding is added
    only if the next element would be misaligned.

    One strategy to minimize memory taken by a structure is to order
    members with the likely strictest alignment first:

    long double
    double
    long long
    pointers
    long
    float
    int
    short
    char

    Now, the above list is a guess based on typical implementations,
    and it will vary somewhat with the implementation. Because of size
    limits in the C Standard, (and alignment often requires alignment
    to a multiple of the size, but the C Standard does not require
    that), the ordering:

    long double >= double >= float
    and
    long long >= long >= int >= short >= char
    will likely stay put.


  6. Re: Optimizing structure memory allocation

    Gordon Burditt wrote:
    >[... good stuff ...]
    >
    > alignment often requires alignment
    > to a multiple of the size, but the C Standard does not require
    > that [...]


    The alignment requirement for a type T must be a *divisor*
    of sizeof(T) -- you can see this by considering the addresses
    of the two elements of `T array[2]'.

    So Gordon's right, sort of: C does not require that the
    alignment be a multiple of the sizeof, but in the case where
    it does happen to be a multiple the multiplier must be unity!

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

+ Reply to Thread