With a variable name other than (void) in C

in the RTEMS initialization procedure, I see this code below.

void boot_card(const char *cmdline) { rtems_interrupt_level bsp_isr_level; /* * Special case for PowerPC: The interrupt disable mask is stored in SPRG0. * It must be valid before we can use rtems_interrupt_disable(). */ #ifdef PPC_INTERRUPT_DISABLE_MASK_DEFAULT ppc_interrupt_set_disable_mask( PPC_INTERRUPT_DISABLE_MASK_DEFAULT ); #endif /* PPC_INTERRUPT_DISABLE_MASK_DEFAULT */ /* * Make sure interrupts are disabled. */ (void) bsp_isr_level; // <--- rtems_interrupt_disable( bsp_isr_level ); -- continues-- 

In the above code, at the beginning, bsp_isr_level is declared as the type rtems_interrupt_level (which is ultimately defined as unsigned int).
But what does the (void) bsp_isr_level; line do (void) bsp_isr_level; ? (indicated by the letter // <- above). This is not a variable passed as an argument to a function, as in here .

EDIT: I found that in my case the variable is assigned by the rtems_interrupt_disable function (in fact it is a macro C # defined), so it is not "not used". But, although assigned, the assigned values ​​do not seem to be used. I do not know if this syntax is also used for such cases (a value is assigned but not used). By the way, I found in the RTEMS source tree a function (a valid function, not #defined) rtems_interrupt_disable, having a void argument, as shown below. (in cpukit / rtems / src / intrbody.c). (#defined version is in cpukit / rtems / include / rtems / rtems / intr.h)

 rtems_interrupt_level rtems_interrupt_disable( void ) { rtems_interrupt_level previous_level; _ISR_Disable( previous_level ); return previous_level; } 

So, perhaps this syntax could be used just in case, if this is the second definition (the value is passed as a void function). I think because this second definition exists, which can be used in some assembly cases.

+7
c rtems
source share
1 answer

It does nothing.

Casting a variable name to (void) is a common way of saying throw it away, still referring to the named variable.

This is usually done inside functions to β€œuse” arguments that otherwise cause a warning about an unused argument or variable.

In this case, it looks redundant and can be left refactoring.

I dug up their publicly available Git a bit (I don't know RTEMS either), but it's impossible to start blame without making a local clone, it seems. From a glance at the head version of the file, it seems clear that there are no preprocessing tricks surrounding this code; it looks like quoted.

+10
source share

All Articles