我正在尝试删除,realloc
但它没有按我预期的方式工作......
void deallocates(int** v, int size, int original_size) {
int i;
*v = (int*)realloc(*v, sizeof(int) * size);
printf("\nAFTER REALLOC FOR SIZE = %d\n", size);
for (i = 0; i < original_size; i++) {
printf("%d ", (*v)[i]);
}
}
int main() {
int i, *v, size, original_size;
srand(time(NULL));
printf("size of the vector: ");
scanf("%d", &original_size);
v = (int *)malloc(sizeof(int) * original_size);
printf("before realloc...\n");
for (i = 0; i < original_size; i++) {
v[i] = rand() % 100;
printf("%d ", v[i]);
}
size = original_size;
for (i = 1; i < size; i++)
deallocates(&v, size - i, original_size);
}
我想删除的值有时会保留。请在我的代码输出中查看这张照片。我在让我烦恼的线条上画了一个红色标记: https ://ibb.co/C1TMHF5