Waitrud Weber’s blog

things and reminders for memories

Code Analyzer: Ho do we use string function in C++ in application

The rule of Not dropping application in C++ is only the below: 

1. Use allocation like new.

so, we must pass Instance to other function as pointer.

 

//  aToken *iToken; <- do not use like this.

aToken *iToken = nullptr; // <- use like this.
 char *parse_token;
 int previous_index = 0;

 iToken = new aToken();

===============================================

 38 ://
 39 ://
 40 ://
 41 ://
 42 ://
 43 :int parse ( char* filename ) {
 44 :	FILE *fp;
 45 :	aToken *iToken = nullptr;
 46 :	char *parse_token;
 47 :	int previous_index = 0;
 48 :
 49 :	iToken = new aToken();
 50 :	fp = fopen ( filename, "rb" );
 51 :	int file_end = filesize ( fp );
 52 :
 53 :	for( int i=0; igetToken( fp, &i, &file_end );
 58 :		printf("parse_token: |%s|\r\n", parse_token );
 59 :		// this cannot scope the below function, but sucope it in aDebug.cpp at 20190303
 60 :		// DEBUG_SUB( msg, "parse_token: |%s|\r\n", parse_token ) ;
 61 :		// DEBUG ( msg, "parse_token: |%s|\r\n", parse_token ) ;
 62 :		// o debug_msg_001 ();
 63 :		// DEBUG_002 ( msg, "parse_token: |%s|\r\n", parse_token ) ;
 64 :		// printf("msg: |%s|\r\n", msg );
 65 :		// err_msg_001 ( "parse_token: |%s|", parse_token ) ;
 66 :
 67 :		if ( m_compare( parse_token, (char *) "void" ) == 1 ) {
 68 :			printf("void\r\n");
 69 :			exit( -1);
 70 :		} else {
 71 ://			m_free_token( parse_token );
 72 ://			free_main_token (); //scope
 73 :			iToken->free_main_token ();
 74 ://			free( parse_token );
 75 ://			token = (char*)"\0";
 76 :		}
 77 :
 78 :		// DEBUG
 79 :		if ( previous_index > i ) {
 80 :			printf("previous_index > i\r\n");
 81 :			printf("i=%d previous_index=%d\r\n", i, previous_index );
 82 :			exit(-1);
 83 :		} else if ( previous_index == i ) {
 84 :			printf("i=%d previous_index=%d\r\n", i, previous_index );
 85 :			exit(-1);
 86 :		}
 87 :
 88 :	}
 89 :
 90 :	fclose(fp);
 91 :	return 1;
 92 :}

===============================================