2014-12-06 83 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。)