2017-04-03 47 views
1

所以我的問題是,當我從數組中的一個成員中的字符串分配內存獲取損壞,我不知道爲什麼。 我主要是在結構中動態數組意見輸出

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#define maxsize 512 
#define charsize 100 
typedef struct Books { 
    char* id; 
    char* title; 
    char* author; 
    char* pages; 
    char* year; 
    char* subject; 
} book; 
char* filename; 
int libsize=4; 
int bookcount=1; 
void printbooks(book books[]); 
void printbooksf(book books[]); 
void srchbook(book books[]); 
void delbook(book books[]); 
void pick(book books[]); 
void addbook(book books[]); 
void addbookf(book books[]); 
int main(int argc, char* argv[]){ 
if (argc < 1) 
    return -1; 
filename=argv[1]; 
FILE* fptr; 
char tempstring[maxsize],* token; 
int i=0,ch; 
book *books=NULL; 
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); 
    //starting size for the pointer 
    for(i=0;i<bookcount;i++){ 
    books[i].id=(char*)malloc(charsize); 
    books[i].title=(char*)malloc(charsize); 
    books[i].author=(char*)malloc(charsize); 
    books[i].pages=(char*)malloc(charsize); 
    books[i].year=(char*)malloc(charsize); 
    books[i].subject=(char*)malloc(charsize); 
    } 
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,","); 
    strcpy(books[i].id,token); 
    token=strtok(NULL,","); 
    strcpy(books[i].title,token); 
    token=strtok(NULL,","); 
    strcpy(books[i].author,token); 
    token=strtok(NULL,","); 
    strcpy(books[i].pages,token); 
    token=strtok(NULL,","); 
    strcpy(books[i].year,token); 
    token=strtok(NULL,","); 
    strcpy(books[i].subject,token); 
    } 
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); 
    return 1; 
} 

現在輸出我從printbooks得到的是

Treasure Island 
Heir to The Empire 
Plumbing for Dummies 
Berserk(!!!!) 
The Troll Cookbook : Human Delights 
Funny Cats 
Linus the Vegetarian T. rex 
Algebra 3 
The Hitchhiker's Guide to the Galaxy 

怎麼過當我使用addbook然後printbook的成員之一被破壞,所以我得到

Treasure Island 
x- 
Plumbing for Dummies 
Berserk(!!!!) 
The Troll Cookbook : Human Delights 
Funny Cats 
Linus the Vegetarian T. rex 
Algebra 3 
The Hitchhiker's Guide to the Galaxy 
the book i added 

addbook是

void addbook(book books[]){ 
    char tempstring[maxsize]; 
    char bugfixer;//gets the /n instead of the temp string 
    bugfixer=getc(stdin); 
    int i; 
    ++bookcount; 
    if(libsize<bookcount){ 
    while(libsize < bookcount){ 
    libsize*=1.5;} 
    books=realloc(books,libsize); 
    } 
    if(books==NULL){ 
     printf("not enough space\n"); 
     exit(-1);} 
    for(i=bookcount-1;i<bookcount;i++){ 
    books[i].id=(char*)malloc(charsize); 
    books[i].title=(char*)malloc(charsize); 
    books[i].author=(char*)malloc(charsize); 
    books[i].pages=(char*)malloc(charsize); 
    books[i].year=(char*)malloc(charsize); 
    books[i].subject=(char*)malloc(charsize); 
    } 
    for(i=bookcount-1;i<bookcount;++i){ 
     printf("add the id\n"); 
     gets(tempstring); 
     strcpy(books[i].id,tempstring); 
     printf("add the title\n"); 
     gets(tempstring); 
     strcpy(books[i].year,tempstring); 
     printf("add the author\n"); 
     gets(tempstring); 
     strcpy(books[i].title,tempstring); 
     printf("add the pages\n"); 
     gets(tempstring); 
     strcpy(books[i].pages,tempstring); 
     printf("add the year\n"); 
     gets(tempstring); 
     strcpy(books[i].author,tempstring); 
     printf("add the subject\n"); 
     gets(tempstring); 
     strcpy(books[i].subject,tempstring); 
    } 
    printf("book number %d added",bookcount); 
    printf("\n"); 
    pick(books); 
} 
` 

和printbook是

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

現在什麼即時試圖在這裏明白的是,爲什麼只有第二個會員被破壞 和爲什麼要任何成員被破壞

+0

停止使用已被棄用且現在已過時的'gets'。 –

+0

'if(argc < 1)' -->'if(argc <2)' – BLUEPIXY

+1

我不明白你爲什麼'malloc'是'struct'中每個'char *'指針的固定內存量,而不是定義每個成員是一個數組,使用'char *'成員的原因是爲了分配所需的* actual *內存,也許使用'strdup'。 –

回答

2

似乎有作爲中指出的其他問題註釋。 但是當你調用addbook時參考的行爲,這種行爲與釋放內存做:

addbook(book books[]),你realloc參數books(即books=realloc(books,libsize)因此,哪個參數books點內存是最有可能被釋放。並指定新的內存位置,但請注意,傳遞給功能addbook的變量仍將指向「舊」並同時釋放內存。指向指針的指針,即book **book,這樣代碼c Alling函數addbook也會得到一個指向新內存塊的指針。 addbook中的代碼必須相應地更改。

+0

我試着做你說的,看來問題仍然存在我做了些什麼騙局 book ** bookptr ; bookptr = &books; ... bookptr = realloc(books,libsize); .... 挑(bookptr); –