Packed array in C

Discussion in 'C Programming' started by Sikandar, Sep 25, 2007.

  1. Sikandar

    Sikandar Guest

    Hi,

    I am beginner in C. Pls let me know what is packed array in C. Where
    is it used?

    Thanks,
    Sikandar
    Sikandar, Sep 25, 2007
    #1
    1. Advertising

  2. Sikandar wrote:
    > Hi,
    >
    > I am beginner in C. Pls let me know what is packed array in C. Where
    > is it used?


    Do you mean packed struct?
    In this case, a idea of packed struct is provided as an extension by
    some compilers. For example GCC provides a packed attribute:

    packed
    The packed attribute 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.

    source:
    http://gcc.gnu.org/onlinedocs/gcc-4.2.1/gcc/Variable-Attributes.html


    >
    > Thanks,
    > Sikandar
    >



    --
    Pietro Cerutti

    PGP Public Key:
    http://gahr.ch/pgp
    Pietro Cerutti, Sep 25, 2007
    #2
    1. Advertising

  3. Sikandar

    Richard Bos Guest

    Sikandar <> wrote:

    > I am beginner in C. Pls let me know what is packed array in C.


    There is no such thing in ISO C. Any packed array extensions you may
    encounter are compiler-specific, and not portable. If you want a
    language where you can pack arrays portably, I believe Pascal lets you
    do so.

    Richard
    Richard Bos, Sep 25, 2007
    #3
  4. Sikandar

    Jack Klein Guest

    On Tue, 25 Sep 2007 15:05:34 GMT, (Richard
    Bos) wrote in comp.lang.c:

    > Sikandar <> wrote:
    >
    > > I am beginner in C. Pls let me know what is packed array in C.

    >
    > There is no such thing in ISO C. Any packed array extensions you may
    > encounter are compiler-specific, and not portable. If you want a
    > language where you can pack arrays portably, I believe Pascal lets you
    > do so.
    >
    > Richard


    Indeed it does, but it doesn't do what many people, apparently
    including you, seem to think.

    In the old, old days, implementations of many languages often wasted
    memory by allocating a machine word to every element of an array, even
    if a machine word was 32 or 36 or60 bits, and the elements of the
    array were of a type that required far fewer bits. Hence the Pascal
    "packed array" of characters.

    That would be equivalent to a plain old ordinary array of
    ((un)signed)char in C.

    --
    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, Sep 25, 2007
    #4
  5. Jack Klein <> writes:
    > On Tue, 25 Sep 2007 15:05:34 GMT, (Richard
    > Bos) wrote in comp.lang.c:
    >> Sikandar <> wrote:
    >> > I am beginner in C. Pls let me know what is packed array in C.

    >>
    >> There is no such thing in ISO C. Any packed array extensions you may
    >> encounter are compiler-specific, and not portable. If you want a
    >> language where you can pack arrays portably, I believe Pascal lets you
    >> do so.

    >
    > Indeed it does, but it doesn't do what many people, apparently
    > including you, seem to think.
    >
    > In the old, old days, implementations of many languages often wasted
    > memory by allocating a machine word to every element of an array, even
    > if a machine word was 32 or 36 or60 bits, and the elements of the
    > array were of a type that required far fewer bits. Hence the Pascal
    > "packed array" of characters.
    >
    > That would be equivalent to a plain old ordinary array of
    > ((un)signed)char in C.


    Not quite. Pascal (at least the version I used) allows packed arrays
    of Boolean, where each element of the array occupies 1 bit. You could
    also have packed arrays of, for example, a 2-bit type.

    --
    Keith Thompson (The_Other_Keith) <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."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Sep 25, 2007
    #5
  6. Sikandar

    Army1987 Guest

    On Tue, 25 Sep 2007 07:45:52 -0700, Sikandar wrote:

    > Hi,
    >
    > I am beginner in C. Pls let me know what is packed array in C. Where
    > is it used?


    There is no such thing as an unpacked array in C. Any array is
    required to be contiguous in memory. For example,
    (char *)&a[1] - (char *)&a[0] is *always* sizeof a[0], by
    definition.
    Types may have padding bits, but they are included also in single
    objects of that type, so they are part of the type's size in all
    aspects.
    Did you mean something else, actually? If so, what?
    --
    Army1987 (Replace "NOSPAM" with "email")
    A hamburger is better than nothing.
    Nothing is better than eternal happiness.
    Therefore, a hamburger is better than eternal happiness.
    Army1987, Sep 25, 2007
    #6
  7. On Sep 25, 7:45 pm, Sikandar <> wrote:
    > Hi,
    >
    > I am beginner in C. Pls let me know what is packed array in C. Where
    > is it used?
    >
    > Thanks,
    > Sikandar


    Coming across such a term in C for the first time :(:(

    Did you call an array in your own words like that ?
    Did you mean "what is an array in c ?"

    Karthik Balaguru
    karthikbalaguru, Sep 26, 2007
    #7
  8. In article <>,
    Keith Thompson <> wrote:

    >Not quite. Pascal (at least the version I used) allows packed arrays
    >of Boolean, where each element of the array occupies 1 bit. You could
    >also have packed arrays of, for example, a 2-bit type.


    An analogy in C would be arrays of bitfields, but unfortunately C does
    not provide these.

    -- Richard

    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
    Richard Tobin, Sep 26, 2007
    #8
  9. Richard Tobin said:

    > In article <>,
    > Keith Thompson <> wrote:
    >
    >>Not quite. Pascal (at least the version I used) allows packed arrays
    >>of Boolean, where each element of the array occupies 1 bit. You could
    >>also have packed arrays of, for example, a 2-bit type.

    >
    > An analogy in C would be arrays of bitfields, but unfortunately C does
    > not provide these.


    They are pretty easy to fake, though.

    --
    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
    Richard Heathfield, Sep 26, 2007
    #9
  10. In article <>,
    Richard Heathfield <> wrote:

    >> An analogy in C would be arrays of bitfields, but unfortunately C does
    >> not provide these.


    >They are pretty easy to fake, though.


    True, but so are for-loops.

    -- Richard
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
    Richard Tobin, Sep 26, 2007
    #10
    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. umberto
    Replies:
    4
    Views:
    568
    umberto
    Oct 24, 2003
  2. Chris
    Replies:
    6
    Views:
    500
    Chris Uppal
    Nov 7, 2004
  3. Sameer
    Replies:
    12
    Views:
    880
    Roedy Green
    Oct 6, 2005
  4. Replies:
    3
    Views:
    799
    Robert Gamble
    Jan 18, 2006
  5. Sergey Emantayev

    calling a function with parameters packed in array

    Sergey Emantayev, Nov 2, 2008, in forum: Javascript
    Replies:
    3
    Views:
    174
    Sergey Emantayev
    Nov 4, 2008
Loading...

Share This Page