Closed SusanTan closed 2 years ago
If I am counting right, isn't your declared copy of the string (the input array variable) a byte too small to hold the text, so this program behavior is undefined?
And the realloc looks definitely a byte too small, so again UB
Oops! Sorry about that.. Yes it's a bug from the benchmark.
The code below produces wrong output. The correct output: g, HHH, 5, YY, ++, ///, \,
the produced output: g, HHH, 5, YY, ++, ///, \
(missing the last comma) I think the produced cbe.c file probably doesn't include '\0' in the argument passed to function "split", so the loop iterates 1 less iteration than the original c code.
include
include
include
char split(char str);
int main(void) { char input[13] = "gHHH5YY++///\"; printf("%s\n", split(input));
return 0; }
char split(char str) { char last = str; char result = malloc(3strlen(str)); char counter = result;
for(char c = str; c; c++) { if(c != last) { strcpy(counter, ", "); counter += 2; last = c; } counter = c; counter++; } *(counter--) = '\0';
return realloc(result, strlen(result)); }