程序显示输出后如何避免回溯错误?

问题描述 投票:0回答:1

这是我正在尝试实施的详细程序:一位博物学家准备去探索亚马逊丛林,并且需要一个计算机程序来记录有关所有发现的新物种的信息。对于每个新物种,都必须存储名称(最多128个字符),大小(实数)和动物类型。哺乳动物,昆虫,鸟类或鱼类)。这是示例运行的样子(键盘输入以斜体显示)...

> NewSpecies
Enter animal information ("exit" to exit)
What is the name : bloatfish
What is the size : 12.47
What is the type : fish
Enter animal information ("exit" to exit)
What is the name : stingybeasty
What is the size : 0.13
What is the type : insect
Enter animal information ("exit" to exit)
What is the name : toothfulsloth
What is the size : 33.33
What is the type : mammal
Enter animal information ("exit" to exit)
What is the name : exit

The following new species were found:
bloatfish            has size  12.47 and is a fish
stingybeasty         has size   0.13 and is a insect
toothfulsloth        has size  33.33 and is a mammal 
You must ...
Implement the program in C.

必须使用结构数组,以便可以将每个新物种记录在数组的元素中。动物的类型表示为枚举类型,表示哺乳动物,昆虫,鸟类或鱼类中的一种。事先未知会发现多少个新的物种,因此程序必须为大小为1的初始数组malloc,并使用加倍的realloc技术按需获取更多内存。您必须始终检查malloc的返回值,就像在Malloc包装函数中一样(或仅使用Malloc:-)。

我的尝试:

    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>

    #define MAX_CHAR 128
    #define LENGTH(A) (sizeof(A)/sizeof(A[0]))

    typedef char String[MAX_CHAR];
    typedef enum {mammal, insect, bird, fish, error} AnimalType;

    typedef struct{
      String name;
      double size;
      AnimalType type;
    } Animal;

    void * Malloc(size_t Size) {

        void * Memory;

        if ((Memory = malloc(Size)) == NULL) {
            perror("Cannot malloc");
            exit(EXIT_FAILURE);
        } else {
            return(Memory);
        }
    }

    AnimalType CheckAnimalType(String type) {
        if (!strcmp(type,"mammal")) {
            return (mammal);
        }
        if (!strcmp(type,"insect")) {
            return (insect);
        }
        if (!strcmp(type,"bird")) {
            return (bird);
        }
        if (!strcmp(type,"fish")) {
            return (fish);
        }
        return (error);
    }

    char *PrintAnimalType(AnimalType type){
      switch(type){
        case mammal: return "mammal"; break;
        case insect: return "insect"; break;
        case bird: return "bird"; break;
        case fish: return "fish"; break;
        case error: return "error";
      }
      return "error";

    }
    void printData(Animal *animal, int size) {
        printf("The following species were found:\n");
        for(int i = 0; i < size-1; i++)
            printf("%s has size %.2lf and is a %s\n", animal[i].name, animal[i].size, PrintAnimalType(animal[i].type));
    }

    void MainMenu(Animal *animal, int *size){
      for(;;){
        Animal newAnimal;
        String animalName;
        double animalSize;
        String animalType;

        printf("Enter animal information (\"exit\" to exit)\n");

        printf("What is the name : ");
        scanf("%s", animalName);
        if(!strcmp(animalName, "exit")) break;
        strcpy(newAnimal.name, animalName);

        printf("What is the size : ");
        scanf("%lf", &animalSize);
        if(newAnimal.size == 0) break;
        newAnimal.size = animalSize;

        printf("What is the type : ");
        scanf("%s", animalType);
        newAnimal.type = CheckAnimalType(animalType);

        if((animal = realloc(animal, sizeof(newAnimal)*((*size)+1))) == NULL) {
                printf("MEMORY ERROR: problem reallocating array\n");
                return;
            }
         animal[(*size)-1] = newAnimal;
            (*size)++;
      }
      printData(animal, *size);
    }



    int main(void) {
      int size = 1;
      Animal *animal = Malloc(sizeof(Animal));
      MainMenu(animal, &size);
      free(animal);
      return 0;
    }

我正在尝试用C实现上述功能,但执行后直接收到此错误:

链接到错误:https://pastebin.com/Wcu0wtet

enter image description here

c pointers memory-management memory-leaks
1个回答
1
投票

MainMenu中,您在realloc上呼叫animal。这可能会调整现有缓冲区的大小,但通常会分配一个新的on,从而更改指针的值。新指针存储在本地animal变量中,而不存储在main中。

返回到main时,调用free(animal),这将尝试释放通过调用realloc已经释放的原始动物缓冲区。

[您将想要将修改后的缓冲区指针作为返回值传递给调用者,或者通过将指针传递给原始变量(Animal **)。

© www.soinside.com 2019 - 2024. All rights reserved.