Compiling C with optimization flag

I am comparing two forms of assembling two C files, one with the optimization flag (-O2) and the other without.

My question is:

Why is this in an optimized version of the assembly, the compiler puts the main function after all the helper functions, while helper functions are included in the source code of the assembly. What does this mean in terms of efficiency? Can anyone comment?

Here is the source file C. Thank you!

// Based on a source file found in a beginner discussion on multidimensional arrays here:
// http://www.dreamincode.net/forums/topic/71617-3d-arrays/

#include <stdio.h>
#include <string.h>

int validColor( char *str );
int mathProblem( char *str );

// 1) Asks from six 'theoretical' different users one of two questions,
// allowing only three answers for each.
// 2) Stores the response into a multidimensional array

int main( void )
{
        char myArray[ 6 ][ 3 ][ 20 ]; /* Six users, three questions, and up to 19 characters per answer
(strings are '\0' terminated), though no "true" answer is that long */
        int i, valid;

/* User instructions */
        for( i = 0; i < 6; i++ )
        {
if ( i % 2 == 0 )
{
valid = 0;
while( valid == 0 )
{
                 printf( "Enter your favorite color : " );

                 fgets( myArray[ i ][ 0 ], 20, stdin ); /* Get answer to first question */

if( myArray[ i ][ 0 ][ 0 ] == '\n' ) /* If the user just presses enter, move to next question */
break; /* Jump out of while loop */

valid = validColor( myArray [i ][ 0 ] ); /* Call function to validate answer */
}
}
if ( i % 2 == 1 )
{
valid = 0;
while( valid == 0)
                        {
                                printf( "The roots of (x - 4)^2 : " );

                                fgets(myArray[ i ][ 0 ], 20, stdin); /* Get answer to second question */

                                if(myArray[ i ][ 0 ][ 0 ] == '\n') /* If the user just presses enter, move to next question */
                                        break; /* Jump out of while loop */

                                valid = mathProblem( myArray[ i ][ 0 ] ); /* Call function to validate answer */
                        }
}
        }

        return 0;
}

int validColor( char *str )
{
if( strcmp( str, "Black\n" ) == 0 )
return 1;
if( strcmp( str, "Red\n" ) == 0 )
return 1;
if( strcmp( str, "Blue\n" ) == 0 )
return 1;

return 0; /* If no match above, answer is not valid */
}

int mathProblem( char *str )
{
if ( atoi( str ) == 2 ) /* Function call for analysis purposes */
return 1;
if ( strcmp( str, "-2\n" ) == 0 )
return 1;
if ( strcmp( str, "+2\n" ) == 0 )
return 1;

return 0; /* If no match above, answer is not valid */
}
+3
source share
2 answers

For efficiency, John is absolutely right. After everything is compiled in the object file, functions are just entries in the symbol table, the order does not matter.

To your question why this order looks like this:

  • , - . , .
  • ( , ) , . , , , , . , - .
+4

.

+3

All Articles