gpt4 book ai didi

无法重新分配结构数组的内存(下一个大小无效)

转载 作者:太空宇宙 更新时间:2023-11-04 04:45:56 24 4
gpt4 key购买 nike

<分区>

我正在尝试重新分配结构数组 abcd_S 但编译器给了我

*** glibc detected *** realloc(): invalid next size: 0x0000000000603010 ***

我想重新分配数组,以便重新分配后没有空字段。另外,您认为我应该在哪里返回或退出程序?

typedef struct abcd {
char *first_field;
char *second_field;
} abcd_S* struct_ptr;

abcd_S* read(int* array_size_ptr){

abcd_S* tmp = NULL;
int j=0,i;
size_t input_len;
struct_ptr =(abcd_S*)malloc(sizeof(abcd_S));

if (struct_ptr == NULL) {
printf("Error: Memory can't be allocated.\n");
}
else {
do {
scanf(format_specifier, input);
if (strcmp(input,"A") != 0){
j++;
if (j == (*array_size_ptr)) {
struct_ptr =(abcd_S*)realloc(struct_ptr, 2 * sizeof(abcd_S));
if (struct_ptr == NULL) {
printf("Error: Memory can't be allocated.\n");
//return((COULDNT_ALLOCATE));
}
*size_ptr = (*size_ptr) * 2; //This needs to be done every time array is full
}
input_len = strlen(input);
struct_ptr[j-1].first_field=(char *)malloc(input_len);
if (struct_ptr[j-1].first_field == NULL) {
printf("Error: Memory can't be allocated.\n");
//return(COULDNT_ALLOCATE);
}
strcpy(struct_ptr[j - 1].first_field, input);

}
else {
abcd_S*tmp = (abcd_S*)realloc(struct_ptr, j * sizeof(abcd_S));
if (tmp == NULL){
printf("Could not reallocate\n");
}
}
}while (strcmp("A",input) != 0);
return(struct_ptr);
}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com