473,472 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Macro to Optimize Space for Embedded Array

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) / 8)
#define TAILSIZE(nbits) ((PAD + 1) > BYTES4BITS(nbits) ? 1 : BYTES4BITS(nbits) - PAD)

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

Thanks,
Mike

Nov 15 '05 #1
4 1690
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) / 8)
#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) / 8)
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.
Nov 15 '05 #2
On Mon, 01 Aug 2005 08:36:14 +0200, Michael Mair wrote:
#define BYTES4BITS(nbits) (((nbits) + 1) / 8)

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) / 8)
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

Nov 15 '05 #3
Michael B Allen <mb*****@ioplex.com> 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
Nov 15 '05 #4
Michael B Allen <mb*****@ioplex.com> 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.
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: marco_segurini | last post by:
Hi, I like to know if this is a good way to remove 'line 18' from compilation when DBG is not defined (I dislike to wrap 'line 18' using #if/#endif pair) of if there are better ways (always...
3
by: John | last post by:
Please, consider this macro: #define mymacro(arg1, arg2) arg1 and arg2 Then it is used: mymacro(boys, girls) How is its expansion?
44
by: Simon Morgan | last post by:
Hi, Can somebody please help me grok the offsetof() macro? I've found an explanation on http://www.embedded.com/shared/printableArticle.jhtml?articleID=18312031 but I'm afraid it still...
4
by: ethan | last post by:
Hi All, I'd like ask some question about macro. If I define a variable set, such as {5, 2, 10} or {1,3}. #define X {5,2,10} 1) Is it possible to write a macro to get the number of items...
0
by: KH Tay | last post by:
Hi, I can able to run the Excel Macro command in VB6. Unfortunately , VB.Net always prompt Array - no declaring. I've insert the Excel references, and it is still not working. Can anyone help me ?...
7
by: d2klein | last post by:
Hi there, i am looking for a macro which replaces a string "string" with its character initialisation form {'s', 't', 'r', 'i', 'n', 'g'}. this should work for strings of different length. Is...
0
by: jalo | last post by:
I need help with this Macro. At the moment I can only use it for the one day specified ie the 1508 but need it to pick up the previous business day. Don't know if there is a simple remedy for this or...
12
by: Martin Wells | last post by:
I'm trying to come up with a fully-portable macro for supplying memset with an unsigned char rather than an int. I'm going to think out loud as I go along. . . I'll take a sample system before I...
5
by: Francois Grieu | last post by:
One of the C compiler that I use <OT>(Keil's CX51)</OTbarks at #define a(b) b int main(void){return a( #if 0 #endif 0);} More generally, this compiler seems confused by any preprocessing...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.