尝试释放char *类型的结构字段时出现“ free():无效指针”错误

时间:2018-10-06 13:25:45

标签: c pointers malloc free

我正在尝试对carinfo_t结构实例中的两个char *字段进行malloc(),然后为free()内存,但是在main()的以下几行上出现“无效指针”错误:

free(carToRemove->brand);
free(carToRemove->model);

以及在freeCarinfo()中:

free(carinfo->brand);
free(carinfo->model);

为什么?我是C的新手,我已经研究了一天,但我不明白怎么了。

这是我的代码:

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

typedef struct carinfo_t
{
    char *brand;
    char *model;
    int year;
    float value;
    struct carinfo_t * next;
} carinfo_t;

struct carinfo_t *createCarinfo(char *brand, char *model, int year, float value);
struct carinfo_t *addCarinfo(struct carinfo_t *carbase, struct carinfo_t *carinfo);
struct carinfo_t *removeCarinfo(struct carinfo_t *carbase, struct carinfo_t *carinfo);
void freeCarinfo(struct carinfo_t *carinfo);


void main()
{

    struct carinfo_t *db = NULL;
    int quit = 0;
    char op;
    char *brand = malloc(25 * sizeof(char));
    char *model = malloc(25 * sizeof(char));
    int year;
    float value;

    struct carinfo_t *car1 = createCarinfo("car1", "model1", 1, 10);
    struct carinfo_t *car2 = createCarinfo("car2", "model2", 2, 80000.00);

    db = addCarinfo(db,car1);
    db = addCarinfo(db,car2);

    while (quit == 0)
    {
        printf("Command (q/r): ");
        scanf(" %c", &op);

        switch (op)
        {
            case 'q':
                quit = 1;
                break;

            case 'r':
                printf(" brand: ");
                scanf(" %[^\n]", brand);
                printf(" model: ");
                scanf(" %[^\n]", model);
                printf(" year: ");
                scanf("%d", &year);

                carinfo_t *carToRemove = createCarinfo(brand, model, year, 0);

                db = removeCarinfo(db, carToRemove);
                free(carToRemove->brand);
                free(carToRemove->model);
                free(carToRemove);
                carToRemove = NULL;
                break;

            default:
                break;
        }
    }

    free(brand);
    free(model);

}

struct carinfo_t *createCarinfo(char *brand, char *model, int year, float value)
{
    carinfo_t *newInfo = (carinfo_t *) malloc (sizeof(newInfo));

    newInfo->brand = malloc (strlen(brand) + 1);
    newInfo->model = malloc (strlen(model) + 1);

    strcpy(newInfo->brand, brand);
    strcpy(newInfo->model, model);
    newInfo->year = year;
    newInfo->value = value;
    newInfo->next = NULL;

    return newInfo;
}

struct carinfo_t *addCarinfo(struct carinfo_t *carbase, struct carinfo_t *carinfo)
{
    carinfo->next = carbase;
    return carinfo;
}

void freeCarinfo(struct carinfo_t *carinfo) 
{
    free(carinfo->brand);
    free(carinfo->model);
    free(carinfo);
}

struct carinfo_t *removeCarinfo(struct carinfo_t *carbase, struct carinfo_t *carinfo)
{
    struct carinfo_t *prev = NULL;
    struct carinfo_t *curr = carbase;

    while(curr != NULL)
    {
        if ( (strcmp(curr->brand,carinfo->brand) == 0) && (strcmp(curr->model,carinfo->model) == 0) && (curr->year == carinfo->year))
        {          
            if (prev == NULL)
            {
                carbase= curr->next;
                prev = curr;
                curr = curr->next;
                freeCarinfo(prev);
            }
            else
            {
                prev->next = curr->next;
                prev = curr;
                curr = curr->next;
                freeCarinfo(prev);
            }
        }
        else
        {
            prev = curr;
            curr = curr->next;
        }
    }

    return carbase;
}

1 个答案:

答案 0 :(得分:0)

出现运行时错误(无效点)的原因在于createCarInfo()函数中。您的代码是:

struct carinfo_t *createCarinfo(char *brand, char *model, int year, float value)
{
    carinfo_t *newInfo = (carinfo_t *) malloc (sizeof(newInfo));
    //  ... rest of your code

在这里,您要求malloc()分配N个字节。在我们的案例中,N是您的newInfo变量的大小。但是,newInfo是指向carinfo_t结构的指针。您可能想做的就是在免费存储区上分配一个carinfo_t结构,并将其地址分配给您的newInfo指针。

因此正确的代码应为:

struct carinfo_t *createCarinfo(char *brand, char *model, int year, float value)
    {
        carinfo_t *newInfo = malloc(sizeof *newInfo);
        //  ... rest of your code

我在PC上编译了您的程序,并试图从数据库中删除汽车,但没有得到运行时错误。

由于您不熟悉C,因此我强烈建议您使用Wall选项编译源代码(启用所有编译器警告)。这将帮助您长期学习和理解C。