Msgstr "misuse of void expression"

I need to write a macro that runs on bitarray, looking like this:

array[0] = number of bits in bitarray (integer) array[1..n] = bits 

The macro should look like this:

 GetBit(pointer, index) Macro *must* return 0,1 or call function similar to exit(). 

This is my (working) built-in macro function that I have to write:

 static inline unsigned long GetBit(BitArray_t array, unsigned long index) { if ((index) >= array[0]) exit(EXIT_FAILURE); else return (GetBit_wo_boundary_checks(array,index)); } 

This is what I have:

 #define GetBit(array,index)\ (((index) < array[0] || exit(EXIT_FAILURE)) ?\ GetBit_wo_boundary_checks(array,index) : 0) 

My problem is that I need to do an index border check (i <p [0]) and exit before trying to access undefined memory using GetBit_wo_boundary_checks (p, i).

I thought I could get around this by putting the output in a short circuit condition, but I get: "invalid use of void expression" .

Is there a way to make this expression a transparent output macro () when the index is greater than the maximum specified in the array [0]?

+4
source share
4 answers

One option is to use a comma operator with your call to exit() , for example:

 (exit(EXIT_FAILURE), 0) 

This is a bit of a hack, but it will make the expression return 0 and should satisfy the compiler.

+6
source

To do this, you can use the operator:

 #define GetBit(array,index) \ (((index) >= array[0]) \ ? exit(EXIT_FAILURE), -1 \ : GetBit_wo_boundary_checks(array,(index))) 

Note that index is evaluated twice, which is unsafe.

+4
source

Have you tried replacing exit(EXIT_FAILURE) with (exit(EXIT_FAILURE), 1) ? Although this is only a hunch, this should do the trick. The expression now yields a value (the right-hand operand of the comma operator is 1, in this case), but first it evaluates the call to exit , which is the part that you really care about.

+2
source

perhaps you could declare a function similar to exit, which calls exit and returns a value.

+1
source

Source: https://habr.com/ru/post/1401611/


All Articles