与使用结构对C分配内存错误 [英] Allocation memory error with use struct for c

查看:119
本文介绍了与使用结构对C分配内存错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个code管理库;编译完成,但在仿真过程中我获得了分配错误(情形2),我不知道为什么。
第一种情况下正常工作,但如果我在第一种情况中输入多个名称,第二种情况下不能正常工作。

我做了什么错?我希望我是很清晰。

  typedef结构{
    焦炭名[80];
    焦炭**书籍;
    INT books_num;
}认购;诠释主(){
    //变量声明:
    INT选项= 0,subs_num = 0,I = 0,books_num = 0;
    认购* subs_library;
    焦炭**书籍;
    炭subs_new_name [80],book_new_name [80];    的printf(请选择一个选项\\ n);
    做{
        scanf函数(%d个,&安培;可选);
        开关(选配件){
          情况1:
            的printf(案例1:输入新的名称\\ n);
            scanf函数(%S,subs_new_name);
            如果(subs_num == 0){
                subs_library =的malloc(sizeof的(认购));
            }其他{
                subs_library =的realloc(subs_library,sizeof的(认购));
            }
            的strcpy(subs_library [subs_num] .name和subs_new_name);
            subs_library [subs_num] .books_num = 0;
            subs_num ++;
            的printf(添加\\ n);
            打破;          案例2:
            的printf(案例2:输入书名\\ n);
            scanf函数(%S,book_new_name);            如果(books_num == 0){
                书=的malloc(sizeof的(字符*));
                书[books_num] =的malloc(80 * sizeof的(炭));
            }其他{
                书= realloc的(书,的sizeof(字符*));
                书[books_num] =的malloc(80 * sizeof的(炭));
            }            如果(书[books_num] == NULL){
                的printf(分配错误。\\ n);
                出口(1);
            }            的strcpy(书[books_num],book_new_name);
            books_num ++;
            的printf(添加\\ n);
            打破;
        }
    }而(选项= 7!);
    返回0;
}


解决方案

您code重新分配数组是不正确。你没有分配足够的空间用于新数组的大小。当您重新分配这些数组,你通过一个单一的元素的大小,所以数组仍然有1而不是的长度subs_num + 1 。传递给的realloc 的大小应该是元素的倍大小以字节为单位的单个元素的。数

初​​始化 subs_library 图书 NULL 和变革阵列重新分配:

 如果(subs_num == 0){
        subs_library =的malloc(sizeof的(认购));
    }其他{
        subs_library =的realloc(subs_library,sizeof的(认购));
    }

进入这个:

  subs_library = realloc的(subs_library,(subs_num + 1)* sizeof的(* subs_library));

和对书做同样的,更改:

 如果(books_num == 0){
        书=的malloc(sizeof的(字符*));
        书[books_num] =的malloc(80 * sizeof的(炭));
    }其他{
        书= realloc的(书,的sizeof(字符*));
        书[books_num] =的malloc(80 * sizeof的(炭));
    }

要这样:

 书籍= realloc的(书,(books_num + 1)* sizeof的(*书));
    书[books_num] =的malloc(80 * sizeof的(炭));

或者更简单的:

 书籍= realloc的(书,(books_num + 1)* sizeof的(*书));
    书[books_num] =的strdup(book_new_name);

I wrote a code for managing a library; the compilation is done but during the simulation I obtained an Allocation error (case2) and I don't know why. The first case works correctly but if I entered more than one name in the first case, the second case doesn't work.

What did I do wrong? I hope I was clear enough.

typedef struct {
    char name[80];
    char **books;
    int books_num;
} Subscription;

int main() {
    // Variables declaration:
    int option = 0, subs_num = 0, i = 0, books_num = 0;
    Subscription *subs_library;
    char **books;
    char subs_new_name[80], book_new_name[80];

    printf("Choose an option\n");
    do {
        scanf("%d", &option);
        switch (option) {
          case 1:
            printf("Case 1: enter a new name\n");
            scanf("%s", subs_new_name);
            if (subs_num == 0) {
                subs_library = malloc(sizeof(Subscription));
            } else {
                subs_library = realloc(subs_library, sizeof(Subscription));
            }
            strcpy(subs_library[subs_num].name, subs_new_name);
            subs_library[subs_num].books_num = 0;
            subs_num++;
            printf("ADDED\n");  
            break;

          case 2:
            printf("Case 2: enter the book name\n");
            scanf("%s", book_new_name);

            if (books_num == 0) {
                books = malloc(sizeof(char*));
                books[books_num] = malloc(80 * sizeof(char));
            } else {
                books = realloc(books, sizeof(char*));
                books[books_num] = malloc(80 * sizeof(char));
            }

            if (books[books_num] == NULL) {
                printf("Allocation Error\n");
                exit(1);
            }

            strcpy(books[books_num], book_new_name);
            books_num++;
            printf("ADDED\n"); 
            break;
        }
    } while (option != 7);
    return 0;
}

解决方案

Your code to reallocate the arrays is incorrect. You do not allocate enough room for the new array sizes. When you reallocate these arrays, you pass the size of a single element, therefore the array still has a length of 1 instead of subs_num + 1. The size passed to realloc should be the number of elements times the size of a single element in bytes.

Initialize subs_library and books to NULL and change your array reallocations:

    if (subs_num == 0) {
        subs_library = malloc(sizeof(Subscription));
    } else {
        subs_library = realloc(subs_library, sizeof(Subscription));
    }

Into this:

    subs_library = realloc(subs_library, (subs_num + 1) * sizeof(*subs_library));

And do the same for books, change:

    if (books_num == 0) {
        books = malloc(sizeof(char*));
        books[books_num] = malloc(80 * sizeof(char));
    } else {
        books = realloc(books, sizeof(char*));
        books[books_num] = malloc(80 * sizeof(char));
    }

To this:

    books = realloc(books, (books_num + 1) * sizeof(*books));
    books[books_num] = malloc(80 * sizeof(char));

Or simpler:

    books = realloc(books, (books_num + 1) * sizeof(*books));
    books[books_num] = strdup(book_new_name);

这篇关于与使用结构对C分配内存错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆