Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Macro to Optimize Space for Embedded Array

Reply
Thread Tools

Macro to Optimize Space for Embedded Array

 
 
Michael B Allen
Guest
Posts: n/a
 
      07-31-2005
Let's say I have a stucture with a flexible array at the end:

struct foo {
short s;
char bits[1];
};

I would like to embed this in other structures like:

#define TAILBITS 500

struct bar {
int i;
struct foo f;
char tail[TAILSIZE(500)];
};

My question is, can someone define a macro TAILSIZE that evaluates to
an optimal value for holding the specified number of bits in foo->bits?

Like a good poster I have tried this beforehand and came up with the
following that takes advantage of any padding in the structure:

#define PAD (sizeof(struct foo) - offsetof(struct foo, bits))
#define BYTES4BITS(nbits) (((nbits) + 1) /
#define TAILSIZE(nbits) ((PAD + 1) > BYTES4BITS(nbits) ? 1 : BYTES4BITS(nbits) - PAD)

At first glance this appears to work but is it legit?

Thanks,
Mike

 
Reply With Quote
 
 
 
 
Michael Mair
Guest
Posts: n/a
 
      08-01-2005
Michael B Allen wrote:
> Let's say I have a stucture with a flexible array at the end:
>
> struct foo {
> short s;
> char bits[1];
> };
>
> I would like to embed this in other structures like:
>
> #define TAILBITS 500
>
> struct bar {
> int i;
> struct foo f;
> char tail[TAILSIZE(500)];
> };
>
> My question is, can someone define a macro TAILSIZE that evaluates to
> an optimal value for holding the specified number of bits in foo->bits?
>
> Like a good poster I have tried this beforehand and came up with the
> following that takes advantage of any padding in the structure:
>
> #define PAD (sizeof(struct foo) - offsetof(struct foo, bits))
> #define BYTES4BITS(nbits) (((nbits) + 1) /
> #define TAILSIZE(nbits) ((PAD + 1) > BYTES4BITS(nbits) ? 1 : BYTES4BITS(nbits) - PAD)
>
> At first glance this appears to work but is it legit?


PAD certainly is okay, as is TAILSIZE (even though I might have
formulated it slightly differently).
If BYTES4BITS is supposed to return the number of bytes necessary
to hold a certain number of bits, you have to try again:
Say you want to hold 9 Bits, then BYTES4BITS tells you one Byte
was quite enough. I.e. you need
#define BYTES4BITS(nbits) (((nbits) + 7) /
or, better yet
#define BYTES4BITS(nbits) (((nbits) + (CHAR_BIT-1)) / CHAR_BIT)

Note that there is not guarantee whatsoever that the struct hack
works in C89 even though it seems to do so (and probably even on
early versions of the DS9000).


Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
 
 
 
Michael B Allen
Guest
Posts: n/a
 
      08-01-2005
On Mon, 01 Aug 2005 08:36:14 +0200, Michael Mair wrote:

>> #define BYTES4BITS(nbits) (((nbits) + 1) /
>>

> If BYTES4BITS is supposed to return the number of bytes necessary
> to hold a certain number of bits, you have to try again:
> Say you want to hold 9 Bits, then BYTES4BITS tells you one Byte
> was quite enough. I.e. you need
> #define BYTES4BITS(nbits) (((nbits) + 7) /
> or, better yet
> #define BYTES4BITS(nbits) (((nbits) + (CHAR_BIT-1)) / CHAR_BIT)


Ah, right. Thanks.

Actually the CHAR_BIT thing confuses me a little. I thought char was
guaranteed to be 8 bits? Are there some little 7 bit micro controllers
that can only hold 7 bits per char?

Thanks,
Mike

 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      08-01-2005
Michael B Allen <(E-Mail Removed)> writes:

> Actually the CHAR_BIT thing confuses me a little. I thought char was
> guaranteed to be 8 bits? Are there some little 7 bit micro controllers
> that can only hold 7 bits per char?


char is at least 8 bits. It may be larger.
--
"This is a wonderful answer.
It's off-topic, it's incorrect, and it doesn't answer the question."
--Richard Heathfield
 
Reply With Quote
 
Tim Rentsch
Guest
Posts: n/a
 
      08-03-2005
Michael B Allen <(E-Mail Removed)> writes:

> Let's say I have a stucture with a flexible array at the end:
>
> struct foo {
> short s;
> char bits[1];
> };
>
> I would like to embed this in other structures like:
>
> #define TAILBITS 500
>
> struct bar {
> int i;
> struct foo f;
> char tail[TAILSIZE(500)];
> };
>
> My question is, can someone define a macro TAILSIZE that evaluates to
> an optimal value for holding the specified number of bits in foo->bits?
>
> [...snip proposed definition...]


(Slight aside: probably better to use 'unsigned char' rather than
'char' for the bits, since with 'unsigned char' all values are
guaranteed to be available.)

In response to the question -- how about finessing the problem
instead?

#define BITMAP_STRUCT(n) \
struct { \
short s; \
unsigned char bits[ BYTES_FOR_BITS(n) ]; \
}

#define BYTES_FOR_BITS(n) ( ((n) + (CHAR_BIT-1)) / CHAR_BIT)

...

struct bar {
int i;
BITMAP_STRUCT( 500 ) f;
};


Unless there is a need to deal with 'struct foo' outside
the context of the other structs where they are embedded,
this approach seems to avoid all the difficulties.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
embedded hash in array to selected-array Karácsony K. Ruby 3 10-26-2010 02:55 PM
Why Python style guide (PEP-8) says 4 space indents instead of 8 space??? 8 space indents ever ok?? Christian Seberino Python 21 10-27-2003 04:20 PM
Re: Why Python style guide (PEP-8) says 4 space indents instead of8 space??? 8 space indents ever ok?? Ian Bicking Python 2 10-24-2003 11:15 AM
Re: Why Python style guide (PEP-8) says 4 space indents instead of8 space??? 8 space indents ever ok?? Ian Bicking Python 2 10-23-2003 07:07 AM
Stack space, global space, heap space Shuo Xiang C Programming 10 07-11-2003 07:30 PM



Advertisments