.exe在程序运行时出现停止工作错误(c程序)



就像你在这个程序中看到的那样。当我编译这个程序时,编译得很好。当我运行这个程序程序上升。exe已停止工作。如果我注释insert()函数调用行,那么程序编译和运行良好。

#include<stdio.h>
#include<stdlib.h>
struct ArrayADT {
int capacity;
int lastIndex;
int * ptr;
};
struct ArrayADT * createArray(int cap) {
struct ArrayADT * arr;
arr = malloc(sizeof(struct ArrayADT));
arr -> ptr = malloc(cap * sizeof(int));
arr -> lastIndex = -1;
arr -> capacity = cap;
return arr;
}
void append(struct ArrayADT * arr, int data) {
if (arr -> lastIndex == arr -> capacity - 1) {
growArray(arr, arr -> capacity * 2, data);
append(arr, data);
} else {
arr -> lastIndex += 1;
arr -> ptr[arr -> lastIndex] = data;
}
}
void growArray(struct ArrayADT * arr, int cap) {
arr -> ptr = realloc(arr -> ptr, cap * sizeof(int));
arr -> capacity = cap;
}
void insert(struct ArrayADT * arr, int index, int data) {
int i;
if (arr -> lastIndex < 0 || index > arr -> lastIndex + 1) {
printf("n Invalid Index.......");
} else if (arr -> lastIndex == arr -> capacity - 1) {
growArray(arr, arr -> capacity * 2);
insert(arr, index, data);
} else {
for (i = arr -> lastIndex; arr -> lastIndex >= index; i--) {
arr -> ptr[i + 1] = arr -> ptr[i];
}
arr -> lastIndex += 1;
arr -> ptr[index] = data;
}
}
void removeItem(struct ArrayADT * arr, int index) {
if (index < 0 || index > arr -> lastIndex) {
printf("n invalid index... please give valid index for remove");
} else {
for (int i = index; i < arr -> lastIndex; i++) {
arr -> ptr[i] = arr -> ptr[i + 1];
}
arr -> lastIndex -= 1;
}
}
void display(struct ArrayADT * arr) {
for (int i = 0; i <= arr -> lastIndex; i++) {
printf("%d, ", arr -> ptr[i]);
}
}
void freeMemory(struct ArrayADT * arr) {
free(arr -> ptr);
free(arr);
}
int main() {
struct ArrayADT * arr;
arr = createArray(3);
append(arr, 10);
append(arr, 15);
append(arr, 8);
append(arr, 20);
append(arr, 25);
insert(arr, 2, 12); // when I comment this line then progrom runs fine
display(arr);
printf("n total capacity = %d", arr -> capacity);
freeMemory(arr);
return 0;
}

首先,有一个错误:

"函数growArray(…)调用的参数太多,预期为2个,有3个">

错误出现在insert(…)的for循环中。我将在这里附加代码:

#include<stdio.h>
#include<stdlib.h>
struct ArrayADT {
int capacity;
int lastIndex;
int *ptr;
};
struct ArrayADT * createArray(int cap) {
struct ArrayADT *arr;
arr = malloc(sizeof(struct ArrayADT));
arr-> ptr = malloc(cap * sizeof(int));
arr->lastIndex = -1;
arr->capacity = cap;
return arr;
}
void growArray(struct ArrayADT *arr, int cap) {
arr->ptr = realloc(arr->ptr, cap * sizeof(int));
arr->capacity = cap;
}
void append(struct ArrayADT *arr ,int data) {
if(arr->lastIndex == arr->capacity - 1) {
growArray(arr, arr->capacity * 2);
append(arr, data);
}
else {
arr->lastIndex += 1;
arr->ptr[arr->lastIndex] = data;
}
}
void insert(struct ArrayADT *arr, int index, int data) {
int i;
if(arr->lastIndex < 0 || index > arr->lastIndex + 1)
printf("n Invalid Index.......");
else if(arr->lastIndex == arr->capacity-1) {
growArray(arr, arr->capacity * 2);
insert(arr, index, data);
}
else {
for(i = arr->lastIndex; arr->lastIndex < index - 1; i--)
arr->ptr[i + 1] = arr->ptr[i];
arr->lastIndex += 1;
arr->ptr[index] = data;
}
}
void removeItem(struct ArrayADT *arr, int index) {
if(index < 0 || index > arr->lastIndex)
printf("n invalid index... please give valid index for remove");
else {
for(int i = index; i < arr->lastIndex ; i++)
arr->ptr[i] = arr->ptr[i+1];
arr->lastIndex -= 1;
}
}
void display(struct ArrayADT *arr) {
for(int i = 0; i <= arr->lastIndex; i++)
printf("%d, ", arr->ptr[i]);
}
void freeMemory(struct ArrayADT *arr) {
free(arr->ptr);
free(arr);
}
int main() {
struct ArrayADT *arr;
arr = createArray(3);
append(arr, 10);
append(arr, 15);
append(arr, 8);
append(arr, 20);
append(arr, 25);
insert(arr, 5, 12);
display(arr);
printf("n total capacity = %d", arr->capacity);
freeMemory(arr);
return 0;
}

请注意,为了更清楚,我尽量使它与您的代码相似。for循环中的问题是:

  • 你应该增加"i"而不是减少它
  • 退出条件为<=

此外,正如0___________建议的那样,请务必阅读警告!

欢呼,丹尼

相关内容

  • 没有找到相关文章

最新更新