Main Content

Function not reachable

Function is called from unreachable part of code

Description

This check appears on a function definition. The check appears gray if the function is called only from an unreachable part of the code. The unreachable code can occur in one of the following ways:

  • The code is reached through a condition that is always false.

  • The code follows a break or return statement.

  • The code follows a red check.

If your code does not contain a main function, this check is disabled

Note

This check is not turned on by default. To turn on this check, you must specify the appropriate analysis option. For more information, see Detect uncalled functions (-uncalled-function-checks).

Examples

expand all

#include<stdio.h>
#define SIZE 100

void increase(int* arr, int index);

void printError()
{
  printf("Array index exceeds array size.");
}

void main() {
  int arr[SIZE],i;
  for(i=0; i<SIZE; i++)
    arr[i]=0;

  for(i=0; i<SIZE; i++) {
    if(i<SIZE)
      increase(arr,i);
    else
      printError();
  }
}

In this example, in the second for loop in main, i is always less than SIZE. Therefore, the else branch of the condition if(i<SIZE) is never reached. Because the function printError is called from the else branch alone, there is a gray Function not reachable check on the definition of printError.

#include<stdio.h>

int getNum(void);

void printSuccess()
{
  printf("The operation is complete.");
}

void main() {
  int num=getNum(), den=0;
  printf("The ratio is %.2f", num/den);
  printSuccess();
}

In this example, the function printSuccess is called following a red Division by Zero error. Therefore, there is a gray Function not reachable check on the definition of printSuccess.

#include<stdio.h>
#define MAX 1000
#define MIN 0

int getNum(void);

void checkUpperBound(double ratio) 
{
    if(ratio < MAX)
        printf("The ratio is within bounds.");
}

void checkLowerBound(double ratio) 
{
    if(ratio > MIN)
        printf("The ratio is within bounds.");
}

void checkRatio(double ratio) 
{
    checkUpperBound(ratio);
    checkLowerBound(ratio);
}

void main() {
    int num=getNum(), den=0;
    double ratio;
    ratio=num/den;
    checkRatio(ratio);
}

In this example, the function checkRatio follows a red Division by Zero error. Therefore, there is a gray Function not reachable error on the definition of checkRatio. Because checkUpperBound and checkLowerBound are called only from checkRatio, there is also a gray Function not reachable check on their definitions.

#include<stdio.h>

int getNum(void);
int getChoice(void);

int num, den, choice;
double ratio;

void display(void) 
{
    printf("Numerator = %d, Denominator = %d", num, den);
}

void display2(void) 
{
    printf("Ratio = %.2f",ratio);
}


void main() {
    void (*fptr)(void);

    choice = getChoice();
    if(choice == 0)
        fptr = &display;
    else
        fptr = &display2;

    num = getNum();
    den = 0;
    ratio = num/den;

    (*fptr)();
}

In this example, depending on the value of choice, the function pointer fptr can point to either display or to display2. The call through fptr follows a red Division by Zero error. Because display and display2 are called only through fptr, a gray Function not reachable check appears on their definitions.

Check Information

Group: Data flow
Language: C | C++
Acronym: FNR