Void function throws no return warning

So I have the following function, apparently void:

static void *CpuStatsTestLoop (void *arg){
    UNUSED (arg);

    rtems_object_set_name(rtems_task_self(), "CPU Usage Thread");

    while (1)
    {
        sleep (CPU_USAGE_REPORT_INTERVAL_SECS);
        rtems_cpu_usage_report();
        rtems_cpu_usage_reset();
    }  
}

      

and he throws

"cpu_stats.c: 98: 1: warning: no return statement in function returning non-void [-Wreturn-type]".

I tried adding an empty return and returning 0 with no luck.

Any idea why she is throwing this error and how to fix it?

+3


source to share


2 answers


This is not a function void

, it is a pointer void*

( void

pointer). It must return a value, which must be a pointer to data of any type, or NULL

.

In your case, a is return

not required because the function never returns: it has a loop while(1)

that runs forever. The best approach is to make it a void

function, not a function void*

, if it doesn't have to conform to some predefined function pointer type.



If changing the return type is not an option, for example because you must pass this function as a parameter, start_routine

pthread_create

you can disable the warning by adding return NULL

to the end of the function body.

+9


source


This function has a return type void *

, that is, a pointer of any type, not void

, so it must return a value.

You can fix this by changing the return type to void

. However, it looks like this function is meant to be called as a stream, in which case it must be signed void *(*)(void *)

, so if the case changing the return type is not an option.



Given that this function has a loop while (1)

, it should never return. However, the function should return something, so put it return NULL;

at the bottom. This will satisfy the compiler and act as a defensive capture if you later present an error that forces you to exit the loop.

+1


source







All Articles