Default return value for when return() is not explicitly used on SC5.0

Default return value for when return() is not explicitly used on SC5.0

Post by Nic » Sat, 02 Feb 2002 06:50:54



I wrote the routine below to test some code in one of our
applications. This snippet exactly mirrors what is happening in our
real code. It was compiled on a sun-4 ultra platfrom using SC5.0 under
Solaris 5.6. If I pass the integer value of 2 as the first arguement
then control falls through the switch statement, and out of the
routine without explicitly returning any value. The compiler does not
report this as either a warning or an error. So far I have not been
unable to find anywhere in the documentation that specifies what the
return value from a routine will be if it is not explicitly given a
value to return. In my test, I wrote a short main to pass the values
(1,1), (1,2), and(2,1) to switchTest(int, int). The first 2 calls
returned the predictable results of TRUE and FALSE. The third call
also returned a FALSE, but that does not satisfy me that the return
value will always be FALSE.

bool switchTest(int num, int num2)
{
  switch(num) {
    case 1:
      switch(num2) {
      case 1:
        printf("CASE 1: Num = %d, Num2 = %d\n", num, num2);
        return(TRUE);
        break;
      default:
        printf("DEFAULT CASE: Num = %d, Num2 = %d\n", num, num2);
        return(FALSE);
      }
  }

Quote:}

Does anybody have the precise information on what the behaviour for
this compiler is defined to be for this situation?

Nick

 
 
 

Default return value for when return() is not explicitly used on SC5.0

Post by Casper H.S. Dik - Network Security Engine » Sat, 02 Feb 2002 18:52:46


[[ PLEASE DON'T SEND ME EMAIL COPIES OF POSTINGS ]]


>Does anybody have the precise information on what the behaviour for
>this compiler is defined to be for this situation?

The behaviour is "undefined"; your code is not complete and the function can
return any garbage value.

The compiler does not appear to catch this (even with -c it only finds
the return()/break anomaly (statement not reached)

lint, which comes with the compiler, does detect it.

Casper
--
Expressed in this posting are my opinions.  They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.

 
 
 

Default return value for when return() is not explicitly used on SC5.0

Post by David William » Tue, 05 Feb 2002 06:35:11



Quote:> I wrote the routine below to test some code in one of our
> applications. This snippet exactly mirrors what is happening in our
> real code. It was compiled on a sun-4 ultra platfrom using SC5.0 under
> Solaris 5.6. If I pass the integer value of 2 as the first arguement
> then control falls through the switch statement, and out of the
> routine without explicitly returning any value. The compiler does > Does

anybody have the precise information on what the behaviour for
Quote:> this compiler is defined to be for this situation?

  It is undefined. Use lint to check for things like this.
Quote:> Nick