2014-12-06 81 views
-3

我用這個代碼,通過這種結構,即時通訊設法使功能來添加項目到這個結構realloc()的無效NXT大小

typedef struct goods{ 
    char *name; 
    int num; 
} goods; 

void addWord(char *what, goods *where, int pnr, int *arrsize, int n){    
    if (pnr >= *arrsize){ 
     where = (goods*)realloc(where,*arrsize*2*sizeof(goods*)); 
     *arrsize*=2; 
    } 
    where[pnr].name = (char*)malloc(strlen(what)*sizeof(char)); 
    strcpy(where[pnr].name,what); 
    where[pnr].num = n; 
} 

在主要功能的陣列我有這個:

int extstore = 1; 
goods *store = (goods*)malloc(1*sizeof(goods*)); 

    addWord(line, store, nr, &extstore, n); 

爲什麼我在addWord()的行where = (goods*)realloc(where,*arrsize*2*sizeof(goods*));上得到「無效的下一個大小」運行時錯誤?

編輯:

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

typedef struct goods{ 
    char *name; 
    int r; 
} goods; 

int main() 
{ 
    int linelen, i, nr = 0, current_r; 
    char *line = NULL; 
    size_t len = 0; 
    int extstore = 1; 
    goods *store; 
    store = malloc(extstore*sizeof(goods*)); 

    while (1){ 
     while ((linelen = getline(&line, &len, stdin)) != -1){ 
      if (line[linelen - 1] == '\n'){ 
       line[linelen - 1] = '\0'; 
      } 

      linelen = strlen(line); 

      if (line[0] == '#'){ 
       if (sscanf(line,"#%d",&current_r) != 1){ 
        printf("bad input."); 
        return 0; 
       } else continue; 
      } 

      if (nr >= extstore){ 
       store = realloc(store,extstore * sizeof(goods*) * 2); 
       extstore*=2; 
      } 

      store[nr].name = malloc(strlen(line)*sizeof(char)); 
      strcpy(store[nr].name,line); 
      store[nr].r = current_r; 

      nr++; 
     } 
     if (linelen == -1) break; 
    } 

    printf("\n"); 
    for (i = 0;i < nr;i++){ 
     printf("%s, [id:%d]\n", store[i].name, store[i].r); 
    } 
    return 0; 
} 
+2

你忘了問一個問題。 – Mureinik 2014-12-06 16:02:44

+0

@Mureinik:他問了一個問題。這是「爲什麼我的代碼中出現錯誤,在指定的行?」。誠然,對問號沒有明確的問題是一個壞主意。至少他提供了所有的信息來弄清楚什麼是錯的... – Deduplicator 2014-12-06 16:10:46

+0

你得到了什麼樣的錯誤? – 4pie0 2014-12-06 16:14:11

回答

1
extstore * sizeof(goods*) * 2 

應該extstore * sizeof(goods) * 2因爲結構的空間應該分配 - 不僅僅是爲指針。

你的代碼中存在一個基本問題。您正在通過值傳遞指針,這意味着對指針(不是指向的變量,而是指針本身)所做的任何更改都不會從函數外部可見。您應該通過指針傳遞指針,而您應該檢查從realloc返回的結果。其次,不要將realloc的結果返回給同一個指針 - 如果失敗,你將失去指向內存的指針 - >因此會發生內存泄漏。

要通過指針傳遞指針:

void addWord(char *what, goods **where, size, ...) { 
    if (*where == NULL) return; // nothing to do 
    if (size < 1) return;  // it would result in realloc=free call 
    goods *res = NULL; 
    res = realloc(*where, size * sizeof(goods)); 

    if (res != NULL) { 
    *where = res; 
    } 
    else { 
    // Error (re)allocating memory 
    // If realloc() fails the original block is left untouched, 
    // it is not freed or moved, so here *where is unchanged 
    } 

還有在C不需要從malloc投的結果。

*錯誤在'路徑':realloc的():無效的下一個大小:0x0000000000ec8010 *

此故障一定是因爲‘這裏’是無效的,由於先前在執行堆損壞。

+0

我如何傳遞指針作爲指針? – lllook 2014-12-06 16:22:46

+0

我不明白,我在addWord函數中改變了arrsize,並且它也在那個函數之外改變了。 – lllook 2014-12-06 16:28:40

+0

其中=(goods *)realloc(其中,* arrsize * 2 * sizeof(goods *));你爲「where」分配新值,但「where」是按值傳遞 - >因此新值被分配給「where」的副本,並且外部函數「where」將指向舊的(現在不正確的)地址 – 4pie0 2014-12-06 16:40:19

0

C是通過按值。

這意味着更改函數中的參數並不會更改從中初始化的表達式。

因此,第一次realloc移動存儲器,主要的指針會壞。

若要更正該問題,請使用額外的間接級別,或者最好返回新值作爲結果。

(無論如何,你應該檢查分配失敗(mallocrealloc),
you should not cast from void* to any pointer-type in C。)