【问题标题】:regarding dynamic allocation array of a structs and sending it between functions关于结构的动态分配数组并在函数之间发送
【发布时间】:2017-04-06 11:49:59
【问题描述】:

我有一些问题,我想知道我做错了什么,是否有人可以向我解释有关 realloc 和 malloc 函数的更多细节。 我遇到的问题来自我的主要问题:

typedef struct Books {
   char *id;
   char *title;
   char *author;
   char *pages;
   char *year;
   char *subject;
} book;
char* filename;
int bookcount=1;
int libsize=4;
int main(int argc, char* argv[]){
 if (argc < 2)
    return -1;
 filename=argv[1];
 FILE* fptr;
 book* books;
 char tempstring[maxsize],* token;
 int i=0,ch;
 fptr=fopen(filename,"r");
 if(fptr==NULL)
    return-1;
//this count how many books are in the file
  while(ch!= EOF){
    ch=fgetc(fptr);
    if(ch == '\n')
    ++bookcount;
  }
 fclose(fptr);
 while(libsize<bookcount){
    libsize *= 1.5;
 }
 books=(book*) malloc(libsize*sizeof(book));
 if(books==NULL)
    exit(-1);

 fptr=fopen(filename,"r");
 if(fptr==NULL)
    return-1;
//this gets all the books into the book array
  for(i=0;i<bookcount;i++){
    fgets(tempstring,maxsize,fptr);
    token=strtok(tempstring,",");
    ch=strlen(token);
    books[i].id=(char*)malloc(ch+1);
    strcpy(books[i].id,token);
    token=strtok(NULL,",");
    ch=strlen(token);
    books[i].title=(char*)malloc(ch+1);
    strcpy(books[i].title,token);
    token=strtok(NULL,",");
    ch=strlen(token);
    books[i].author=(char*)malloc(ch+1);
    strcpy(books[i].author,token);
    token=strtok(NULL,",");
    ch=strlen(token);
    books[i].pages=(char*)malloc(ch+1);
    strcpy(books[i].pages,token);
    token=strtok(NULL,",");
    ch=strlen(token);
    books[i].year=(char*)malloc(ch+1);
    strcpy(books[i].year,token);
    token=strtok(NULL,",");
    ch=strlen(token);
    books[i].subject=(char*)malloc(ch+1);
    strcpy(books[i].subject,token);
   }
  books=(book*) realloc(books,libsize*sizeof(book));
  fclose(fptr);
    printf("to add a book press 1\n");
    printf("to delete a book press 2\n");
    printf("to find a book press 3\n");
    printf("to print all books press 4\n");
    printf("to save library in a file press 5\n");
    printf("to add books from a file press 6\n");
    printf("to exit press 0\n");
    pick(books);
    free(books);
    return 1;
}

我将动态分配的结构数组发送到一个函数中,让我选择 现在当我打电话打印书籍时

void printbooks(book* books){
    int i;
    for(i=0;i<bookcount;++i){
        printf("%s\n",books[i].title);
    }
    printf("Fin\n");
    pick(books);
}

我得到了预期的输出 我是如何使用 addbook 功能的

void addbook(book* books){
    char tempstring[maxsize];
    gets(tempstring);
    book* temp;
    int i=bookcount-1,ch;
    ++bookcount;
    if(libsize < bookcount){
    while(libsize < bookcount){
    libsize*=1.5;}
    temp=(book*)realloc(books,libsize);
    }
    if(temp==NULL){
        printf("no more space\n");
        exit(-1);}

    if(temp!=NULL){
        books=temp;}

    printf("add the id\n");
    gets(tempstring);
    ch=strlen(tempstring);
    books[i].id=(char*)malloc(ch+2);
    strcpy(books[i].id,tempstring);
    printf("add the title\n");
    gets(tempstring);
    ch=strlen(tempstring);
    books[i].title=(char*)malloc(ch+2);
    strcpy(books[i].title,tempstring);
    printf("add the author\n");
    ch=strlen(tempstring);
    books[i].author=(char*)malloc(ch+2);
    gets(tempstring);
    strcpy(books[i].author,tempstring);
    printf("add the pages\n");
    gets(tempstring);
    ch=strlen(tempstring);
    books[i].pages=(char*)malloc(ch+2);
    strcpy(books[i].pages,tempstring);
    printf("add the year\n");
    gets(tempstring);
    ch=strlen(tempstring);
    books[i].year=(char*)malloc(ch+2);
    strcpy(books[i].year,tempstring);
    printf("add the subject\n");
    gets(tempstring);
    ch=strlen(tempstring);
    books[i].subject=(char*)malloc(ch+2);
    strcpy(books[i].subject,tempstring);

    printf("book number %d added",bookcount);
    printf("\n");
    pick(books);
}

一些结构成员损坏并且程序崩溃,当我尝试将库大小更改为 20 并在我尝试输入新书的第一个成员时运行它时,程序崩溃了。

【问题讨论】:

  • 这堵代码非常难读。使用空行作为格式化代码的一种方式,将不相关的部分分开。此外,C 语言中没有称为gets 的函数。谁教你用的都是坏人。
  • 标准 C(自 C11 起)不再具有函数 gets(),因为 gets() is too dangerous to be used — ever! 但是,它在 C 库中被广泛使用。即使它被广泛使用,你也应该假设它被实现为char *gets(char *str) { abort(); }。这比尝试从标准输入读取内容的正常实现更安全。
  • 在调用任何堆分配函数(malloc、calloc、realloc)时 1) 在 C 中,不要强制转换返回值。返回值的类型为void*,因此可以分配给任何其他指针。强制转换只会使代码混乱,使其更难以理解、调试等 2) 始终检查 (!=NULL) 返回值以确保操作成功。特殊情况:在赋值给目标指针之前检查返回值,否则如果realloc()失败,原来的指针会丢失,导致内存泄漏。注:贴出的代码检查失败realloc()
  • 这种代码块:if(fptr==NULL) return-1; 最好写成:if( !fptr ) { perror( "fopen to read file failed"); exit( EXIT_FAILURE );,因为这样会导致适当的退出,并且会输出到 stderr 包含的消息字符串和原因操作系统认为操作失败。

标签: c arrays dynamic struct


【解决方案1】:

addbook

void addbook(book* books){
    ..
    libsize*=1.5;}
    temp=(book*)realloc(books,libsize);
    }

realloc 调用未正确调整数组的大小(您在另一个函数中正确执行此操作)。它应该包括字体大小:

temp = realloc(books,libsize*sizeof(book));

否则它会缩小大小并且您会出现损坏/未定义的行为,因为例如内存在其他地方被重用。

一般说明:这些分配函数很棘手且容易出错:最好将它们放在实用程序函数中并且只使用这些函数,不要复制/粘贴此类代码一百万次(例如:malloc(ch+2) 分配了过多的 1 个字节,很多时间)。对于您的代码,它可以使用评论。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    • 2019-07-19
    • 2020-03-06
    • 1970-01-01
    • 1970-01-01
    • 2021-10-30
    • 2017-03-30
    相关资源
    最近更新 更多