Home > Enterprise >  In C language, what's the purpose of statement that is after return?
In C language, what's the purpose of statement that is after return?

Time:09-21

Recently I read the code of one public C library and found below function definition:

void* block_alloc(void** block, size_t* len, size_t type_size)
{
    return malloc(type_size);

    (void)block;
    (void)len;
}

I wonder whether it will arrive at the statements after return. If not, what's the purpose of these 2 statements that convert some data to void ?

CodePudding user response:

As Basil notes, the (void) statements are likely intended to silence compiler warnings about the unused parameters. But - you can move the (void) statements before the return to make them less confusing, and with the same effect.

In fact, there's yet another way to achieve the same effect, without resorting to any extra statements. It's supported by many compilers already today, although it's not officially in the C standard before C2X:

void* block_alloc(void**, size_t*, size_t type_size)
{
    return malloc(type_size);
}

if you don't name the parameters, typical compilers don't expect you to be using them.

CodePudding user response:

First, these statements appearing in the block after a return will never be executed.

Check by reading some C standard like n1570.

Second, on some compilers (perhaps GCC 10 invoked as gcc -Wall -Wextra) the useless statements might avoid some warnings.

In my opinion, coding these statements before the return won't change the machine code emitted by an optimizing compiler (use gcc -Wall -Wextra -O2 -fverbose-asm -S to check and emit the assembler code) and makes the C source code more understandable.

GCC provides, as an extension, the variable __attribute__ named unused.

Perhaps in your software your block_alloc is assigned to some important function pointer (whose signature is requested)

CodePudding user response:

It is used to silence the warnings. Some programming standards required all the parameters to be used in the function body, and their static analyzers will not pass the code without it.

It is added after the return to prevent a generation of the code in some circumstances:

int foo(volatile unsigned x)
{
    (void)x;
    return 0;
}

int foo1(volatile unsigned x)
{
    return 0;
    (void)x;
}
foo:
        mov     DWORD PTR [rsp-4], edi
        mov     eax, DWORD PTR [rsp-4]
        xor     eax, eax
        ret
foo1:
        mov     DWORD PTR [rsp-4], edi
        xor     eax, eax
        ret
  • Related