user8258333
user8258333

Reputation:

Void function is throwing a "no return statement" warning

So I have the following function, evidently a 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 it throws

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

I've tried adding a blank return and return 0 with no luck.

Any idea about why it's throwing this error, and how to fix?

Upvotes: 3

Views: 2473

Answers (2)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726589

This is not a void function, this is a void* (void pointer) function. It must return a value, which must be a pointer to data of any type, or NULL.

In your case, a return is not necessary, because the function never returns: it has a while(1) loop, which runs forever. A better approach is to make it a void function, rather than a void* function, unless it must conform to some predefined function pointer type.

If changing return type is not an option, for example, because you must pass this function as a start_routine parameter of pthread_create you can silence the warning by adding return NULL at the end of the function's body.

Upvotes: 10

dbush
dbush

Reputation: 223972

This function has a return type of void *, i.e. a pointer of any type, not void, so it must return a value.

You could fix this by changing the return type to void. However, it looks like this function is meant to be invoked as a thread, in which case it has to have a signature of void *(*)(void *), so if that's the case changing the return type is not an option.

Given that this function has a while (1) loop it should never return. Still, the function needs to return something, so put return NULL; at the bottom. That will satisfy the compiler, and it will act as a safety catch in case you later introduce a bug that causes you to break out of the loop.

Upvotes: 2

Related Questions