sleep solves the allocation error better than check if that is null or not.
And, we don't blame the people who choose this way.
1 |
function: m_replace requires the base string and replace char1 to char2 in the string and return the replaced result string.
return *char: the most importan part is return global memoried char_string. |
||
2 |
.\array_counter.cpp Sun Mar 21 03:39:36 2021 ,,, 124 :char* m_replace ( char* char_string, 125 : char* from_string, char* to_string ) { 126 : char c1, c2, c11; 127 : int index_max = 0; 128 : char* result; 129 : 130 : err_msg_001("char* m_replace ( char* char_string, char* from_string, char* to_string ) starts.\r\n"); 131 : 132 : int count = array_count( char_string ); 133 : int a_f = array_count( from_string ); 134 : int a_t = array_count( to_string ); 135 : int a_c = 0; 136 : index_max = ( count + a_t - 1 ); 137 : char* char_string_2 = (char *) malloc ( sizeof (char)* index_max ); 138 : 139 : err_msg_001("char_string_2 was allocated p|%p| size|%d|.\r\n", char_string_2, index_max ); 140 : for ( int i=0; i<3; i++) { 141 : if (char_string_2 != NULL ) { 142 : err_msg_001(" it can not allocate memories enough, so, it is sleeping in one second,\r\n"); 143 : sleep(1); 144 : break; 145 : } 146 : } 147 : 148 : if (char_string_2 == NULL ) { 149 : printf("m_replace can not allocate memories enough, so , it exit(-1).\r\n"); 150 : exit(-1); 151 : } 152 : 153 : c2 = 0; 154 : int cnt_replace = 0; 155 : for ( int i = 0; i<count; i++ ) { 156 : err_msg_001("loop starts: "); 157 : c1 = *( char_string + i ) ; 158 : a_c = 0; 159 : for ( int j=0; j |
||