我一直在做一些有趣的编码练习,最近我在 codewars 上遇到了这个挑战。下面列出了我提出的解决方案,但它输出错误(“realloc(): invalid next size”和“Test Crashed - Caught unexpected signal: 6”)。我不知道这里发生了什么,因为 realloc 对我来说似乎完全没问题。有什么线索吗?
谢谢,何塞·阿尔梅达
#include <stdlib.h>
#include <stdio.h>
int* delete_nth(size_t szin, int order[szin], int max_e, size_t *szout) {
int *arr = NULL, *temp, tms;
尺寸_t i,j;
*szout = 0;
对于 (i = 0; i < szin; i++) {
tms = 0;
对于 (j = 0; j < *szout; j++)
if (arr[j] == order[i]) {
tms++;
}
如果(tms < max_e){
temp = realloc(arr, *szout + 1);
if (temp == NULL) 返回 NULL;
arr =温度;
arr[*szout] = order[i];
(*szout)++;
}
}
返回 arr;
}