Lesson 13 Hidden Bug In A Program

Sometimes , you notice that the program has a bug.    But , after checking
the code again and again , you just can not figure the bug out.

You are sure , the logic of the program flow is correct.    But how come the
result surprise you?

Here may have your answer.


Case 1:
for(int i ; i<100 ; i++) { ... }

The code here looks fine , isn't it ?
The problem of this code is : The int i did not been initialized !
The correct syntax is : for(int i=0 ; i<100 ; i++) { ... }


Case 2:
if( intA = 5 ) { ... }

If this code hide in a program contains a lot of if(...) { ... } , you
may not notice the error in here.
The problem of this code is : if function need a true or false return.
The correct syntax is : if( intA == 5 ) { ... }

if( intA == 5 & intB == 1 ) { ... }

This is another similar case.
The correct syntax is : if( intA == 5 && intB == 1 ) { ... }



Case 3:
SDL_Surface *imgTemp ;
AFun( ) {
    imgTemp = TTF_RenderText_Solid( );
    return;
    }

First , define a global variable SDL_Surface pointer.   Then use it to get
the return value from TTF_RenderText_Solid( ).

It looks like we can re-use it again and again.    But this will result in the
memory been eaten little by little.

The correct syntax is :
SDL_Surface *imgTemp ;
AFun( ) {
    imgTemp = TTF_RenderText_Solid( );
    SDL_FreeSurface( imgTemp );
    return;
    }

    .

bdragon
Copyright © 2009 bdragon All rights reserved.
Comments