Packed array in C

S

Sikandar

Hi,

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

Thanks,
Sikandar
 
P

Pietro Cerutti

Sikandar said:
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
 
R

Richard Bos

Sikandar said:
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
 
J

Jack Klein

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
 
K

Keith Thompson

Jack Klein said:
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.
 
A

Army1987

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

karthikbalaguru

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
 
R

Richard Tobin

Keith Thompson said:
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
 
R

Richard Heathfield

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

They are pretty easy to fake, though.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top