tags:

views:

118

answers:

5

Hi all.. i was looking for macro which can expand like the following:

FILL_BUFF(4) should be expanded as (0xFF, 0xFF, 0xFF, 0xFF)... what can be the macro written for the above expansion..

+2  A: 

Macros don't have conditional controls such as loops - they are very simple.

It's common to see a group of macros in a header covering all the common expansions, e.g.


#define FILL_BUFF_1 (0xFF)
#define FILL_BUFF_2 (0xFF,0xFF)
#define FILL_BUFF_3 (0xFF,0xFF,0xFF)
#define FILL_BUFF_4 (0xFF,0xFF,0xFF,0xFF)
PP
If you REALLY wanted to use the FILL_BUFF(4), you could always have `#define FILL_BUFF(n) FILL_BUFF_##n` to reference back to the ones defined here. I must emphasise that I'm not recommending this though (apart from anything else, it breaks MISRA rule 19.13 and breaking MISRA rules is almost always a BAD thing in my opinion).
Al
But i want to have above situation but using macros
inquisitive
@inquisitive: You can't always get what you want.
Michael Carman
A: 

Hmm, maybe via memset:

#define FILL_BUFF(buf, n) memset(buff, 0xff, n)

But I am not sure that is such a good idea

laura
@laura: I think that should be memset(buf, 0xff, n); you added an extra f into the word in the macro! :)
tommieb75
+1  A: 

PP got it - almost. Abusing the C preprocessor again. On the other hand, it deserves nothing better.

#define FILL_BUFF(N) FILL_BUFF_ ## N

#define FILL_BUFF_1 (0xFF)
#define FILL_BUFF_2 (0xFF,0xFF)
#define FILL_BUFF_3 (0xFF,0xFF,0xFF)
#define FILL_BUFF_4 (0xFF,0xFF,0xFF,0xFF)
drhirsch
@drhirsch: even in thought the same. but i cant keep having so much expansions (it might need till 0 to 63)!
inquisitive
0..63 isn't a big set for a header file. I'd only worry about it if the list was more than 1,000 or 10,000 lines long..
PP
+1  A: 

You may want to look at the boost preprocessor library. Especially the BOOST_PP_REPEAT_z macros:

#define DECL(z, n, text) text ## n = n;

BOOST_PP_REPEAT(5, DECL, int x)

results in:

int x0 = 0; int x1 = 1; int x2 = 2; int x3 = 3; int x4 = 4;

In your case you could do:

#define FILL_BUFF_VALUE(z, n, text) text,
#define FILL_BUFF(NPLUSONE, VALUE) { BOOST_PP_REPEAT(NPLUSONE, FILL_BUFF_VALUE, VALUE } VALUE )

int anbuffer[] = FILL_BUFF(4 /* +1 */,0xff); // anbuffer will have length 5 afterwards

which would expand to

int anbuffer[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
Sebastian
Does this work with C?
Remo.D
This is buggy. Currently it expands to "( BOOST_PP_REPEAT_1_4-1(FILL_BUFF_VALUE, 0xff) 0xff )"
drhirsch
Thanks for the comments. It works now but is a bit ugly unless there is a way to have "N-1" evaluate first inside the macros.
Sebastian
A: 

@sebastian: thanx for the link.. but it doesnt work in my C environment. i have tried the below, but somehow doesnt work:

#define X

#define FILL_BUFF (int *)(0xFF, 0xFF, .. 0xFF) // not sure whether syntactically correct

#define LOOKUP \

X(0x13, NULL), \

X(0x14, FILL_BUFF)

#undef X #define X(a,b) b

int *buff_ptr[2] = {LOOKUP};

will i be able to de-reference the elements of buff_ptr.. if so, how?

inquisitive
when it is like #define FILL_BUFF (int *)(0x01, 0x10) in the above macro definition, and i give printf("%x", *buff_ptr), i m getting 0x10 getting displayed!! so it means it is de-referencing.. but when i try to de-reference other elements i am not able to :(
inquisitive