char data[0]

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

    #31
    Re: char data[0]

    pete <pfilandr@minds pring.comwrites :
    [...]
    With suitable padding bytes,
    a struct with a first member of type char,
    could be aligned at any address,
    no matter what type the other members were.
    >
    If you had 4 byte ints and a struct type
    {
    char X;
    int Y;
    }
    >
    you could have
    X followed by 3 padding bytes
    followed by Y
    for a struct alignment where the struct was aligned for type int.
    >
    If the struct was aligned on the next byte then
    X followed by 2 padding bytes
    followed by Y
    followed by 1 padding byte would work.
    >
    If the struct was aligned on the byte after that then
    X followed by 1 padding byte
    followed by Y
    followed by 2 padding bytes would work.
    >
    And if the stuct was aligned on the next byte after that then
    X contiguous with Y
    followed by 3 padding bytes would work.
    Interesting idea. Normally, 4-byte alignment is a requirement that
    address % 4 == 0. (There isn't really a "%" operator for pointers,
    but you get the idea; the standard's definition of alignment,
    "requiremen t that objects of a particular type be located on storage
    boundaries with addresses that are particular multiples of a byte
    address", implies something like this.)

    What you're suggesting is that a type's alignment could require
    alignment % 4 == 3.

    Unfortunately, I think it would make malloc() impossible to implement
    correctly.

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

    • Dave Thompson

      #32
      Re: char data[0]

      On Wed, 11 Oct 2006 12:08:55 -0700, Andrey Tarasevich
      <andreytarasevi ch@hotmail.comw rote:
      aarklon@gmail.c om wrote: <snip>
      typedef struct mall_li_header_ { <snip rest>
      char data[0];
      } mall_li_header_ t;
      <snip>
      Provided the structure is declared as above but with the 'data' array of any
      non-zero size, the total amount of memory needed for a structure with a trailing
      'data' array of size 'n' can be calculated as follows
      >
      size_t size;
      mall_li_header_ t* p;
      ...
      size = offsetof(mall_l i_header_t, data) + sizeof(*p->data) * n;
      >
      Right. (Although for char the sizeof multiplication could be omitted.)
      Unless the compiler aligns differently for different array sizes,
      which I think formally is permitted but would break so much code it
      would be unacceptable -- and is explicitly prohibited for the
      new-in-C99 FAM version which you described but I snipped.
      or as
      >
      size = sizeof(*p) - sizeof(p->data) + sizeof(*p->data) * n;
      >
      But this may waste space. sizeof the whole struct can also include
      trailing padding -- although in this particular example, snipped, it
      _likely_ does not -- and if so, subtracting only sizeof the last
      element gives you a too-high figure. Although that is still safe; the
      accesses to both fixed and variable elements within the malloc'ed
      space will be correctly positioned and work and the extra space
      ignored, unless you do something like memcmp'ing for size to compare
      the entire structs, and that isn't reliable in general anyway.


      - David.Thompson1 at worldnet.att.ne t

      Comment

      Working...