Packed array in C

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

    #1

    Packed array in C

    Hi,

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

    Thanks,
    Sikandar

  • Pietro Cerutti

    #2
    Re: Packed array in C

    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:


    >
    Thanks,
    Sikandar
    >

    --
    Pietro Cerutti

    PGP Public Key:

    Comment

    • Richard Bos

      #3
      Re: Packed array in C

      Sikandar <sikandar032@gm ail.comwrote:
      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

      Comment

      • Jack Klein

        #4
        Re: Packed array in C

        On Tue, 25 Sep 2007 15:05:34 GMT, rlb@hoekstra-uitgeverij.nl (Richard
        Bos) wrote in comp.lang.c:
        Sikandar <sikandar032@gm ail.comwrote:
        >
        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)cha r 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.l earn.c-c++

        Comment

        • Keith Thompson

          #5
          Re: Packed array in C

          Jack Klein <jackklein@spam cop.netwrites:
          On Tue, 25 Sep 2007 15:05:34 GMT, rlb@hoekstra-uitgeverij.nl (Richard
          Bos) wrote in comp.lang.c:
          >Sikandar <sikandar032@gm ail.comwrote:
          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)cha r 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_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."
          -- Antony Jay and Jonathan Lynn, "Yes Minister"

          Comment

          • Army1987

            #6
            Re: Packed array in C

            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.

            Comment

            • karthikbalaguru

              #7
              Re: Packed array in C

              On Sep 25, 7:45 pm, Sikandar <sikandar...@gm ail.comwrote:
              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

              Comment

              • Richard Tobin

                #8
                Re: Packed array in C

                In article <lnmyvazhk6.fsf @nuthaus.mib.or g>,
                Keith Thompson <kst-u@mib.orgwrote:
                >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

                --
                "Considerat ion shall be given to the need for as many as 32 characters
                in some alphabets" - X3.4, 1963.

                Comment

                • Richard Heathfield

                  #9
                  Re: Packed array in C

                  Richard Tobin said:
                  In article <lnmyvazhk6.fsf @nuthaus.mib.or g>,
                  Keith Thompson <kst-u@mib.orgwrote:
                  >
                  >>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

                  Comment

                  • Richard Tobin

                    #10
                    Re: Packed array in C

                    In article <uu2dnXPqmv5Zym fbnZ2dneKdnZydn Z2d@bt.com>,
                    Richard Heathfield <rjh@see.sig.in validwrote:
                    >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
                    --
                    "Considerat ion shall be given to the need for as many as 32 characters
                    in some alphabets" - X3.4, 1963.

                    Comment

                    Working...