Waitrud Weber’s blog

things and reminders for memories

Analyzer: windows-make: sleep solves the allocation error better than check if that is null or not.

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= 0) {
168 :			// match
169 :			int to = i + a_f;
170 :			for( int k=0; k<a_t; k++ ) {
171 :				err_msg_001("replace string k=|%d|%c|\r\n", k, *( to_string + k ));
172 :				int put_index_001 = + i + k + cnt_replace*(a_t- a_f );
173 :				if ( index_max <= put_index_001 ) {
174 :					index_max *= 2;
175 :					char_string_2 = (char*) realloc ( char_string_2, sizeof(char) * index_max );
176 :				}
177 :				*( char_string_2 + put_index_001 ) = 
178 :				  *( to_string + k );
179 :			}
180 :			i = to - 1;
181 :			cnt_replace++;
182 :		} else {
183 :			int put_index_002 = i + cnt_replace*(a_t- a_f );
184 :			if ( index_max <= put_index_002 ) {
185 :				index_max *= 2;
186 :				char_string_2 = (char*) realloc ( char_string_2, sizeof(char) * index_max );
187 :			}
188 :			*( char_string_2 + put_index_002 ) = c1;
189 :		}
190 :	}
191 :	*( char_string_2 + count + cnt_replace*( a_t - a_f ) ) = '\0';
192 :	result = copyof(char_string_2);
193 :
194 :	int countofresult = array_count(result);
195 :
196 :	err_msg_001("char* m_replace ( char* char_string, char* from_string, \
197 :char* to_string ) ends.|%s|count|%d|cnt_replace|%d| countofresult|%d|\r\nresult|%s|\r\nstring|%s|\r\n",
198 : result, count, cnt_replace, countofresult, result, char_string);
199 :
200 :	free(char_string_2);
201 ://	exit(-1);
202 :	return result;
203 :}