realloc():下一个尺寸无效:

时间:2016-03-29 19:55:34

标签: c

我正在研究一个计算文件中单词出现次数的函数,我修剪了我找到的单词(空格分隔),检查它们是否已存在于我的字符串数组中,如果不是,我展开该数组并且添加字符串。

当我尝试编译时,我得到以下内容:

In file included from lsa.c:2:0:
read_file.h: In function 'read_file':
read_file.h:57:13: warning: ignoring return value of 'realloc', declared with attribute warn_unused_result [-Wunused-result]
      realloc(words, (sizeof(char *) * number_of_words));
             ^

当我跑步时:

number of words: 1
ELF
number of words: 2
H������H�������ee@b�g�"6,�&@2016-03-04-21.18PID     : 2529                 TID : 140564200679168 PROC : db2sysc 0
PID
number of words: 3
*** Error in `./a.out': realloc(): invalid next size: 0x00000000020b5010 ***

此时我必须按ctrl + c退出该功能,我给该程序的文件开始于: 2016-03-04-21.18

没有ELF或这些字符,我假设我做错了,可能是我的修剪功能:

void trim(char * word)
{
  if(word[strlen(word) -1 ] < 48 || word[strlen(word) -1 ] > 57 //numeric characters
      && word[strlen(word) -1 ] < 65 || word[strlen(word) -1 ] > 90  //uppercase characters
      && word[strlen(word) -1 ] < 97 || word[strlen(word) -1 ] > 122
    )
  {
    word[strlen(word) -1 ] = '\0';
    if(strlen(word) > 0)
      trim(word);
  }
  if(word[0] < 48 || word[0] > 57 //numeric characters
      && word[0] < 65 || word[0] > 90  //uppercase characters
      && word[0] < 97 || word[0] > 122
      )
  {
    memmove(word, word+1, strlen(word));
    if(strlen(word) > 0)
      trim(word);
  }
}

我有以下代码:

#include<string.h>
#include"trim.h"
#include <stdlib.h>

extern char **words;
extern int number_of_words;

void append(char * string,char ch)
{
    int size=strlen(string);
    char temp[size+1];
    strcpy(temp,string);
    temp[size]=ch;
    temp[size+1]='\0';
    strcpy(string,temp);
}

void read_file(char *file_name , int file_number) 
{
  FILE *file = fopen(file_name,"r");
  char line[2048];
  int i = 0;
  char word[64];
  while (fgets(line, sizeof line, file) != NULL) 
  {
    i = 0;
    while (i < strlen(line) && line[i] != '\n' )
    {
      if (line[i] != ' ')
      {
    append(word,line[i]);
      }
      else 
      {
    if (strlen(word) > 1){
      //trim the word
      trim(word);
      //do your magic
      int a = 0;
      int exists = 0;


      while (a < 1)
      {
        if(strcmp(words[a],word) == 0) //word exists in the words array
        {
          exists = 1;
          printf("%s\n",words[a]);
            }
        a++;
      }

          if (exists < 1)
      {
        printf("number of words: %i\n", number_of_words);
        number_of_words++;
        char **temp = realloc(words, (sizeof(char *) * number_of_words));
        if(temp == NULL) {
              //realloc failed.
                  perror("realloc");
                      exit(EXIT_FAILURE);
                      }
                      words = temp;
        words[number_of_words] = malloc(strlen(word) + 1);
        strcpy(words[number_of_words], word);
      }

       printf("%s\n",word);

      memset(word,0,strlen(word));
    }
      }
      i++;
    }

  }
  fclose(file);
}

初​​始化:

char **words;
words = malloc(9*sizeof(char *));
  words[0] = malloc(strlen("the"));
  strcpy(words[0],"the");

3 个答案:

答案 0 :(得分:4)

realloc可能会返回一个新指针,其中已分配了更多空间。

char **temp = realloc(words, (sizeof(char *) * number_of_words));
if(temp == NULL) {
    //realloc failed.
    perror("realloc");
    exit(EXIT_FAILURE);
}
words = temp;

答案 1 :(得分:4)

realloc

注意realloc的细微差别,始终使用临时指针。如果NULL失败,则临时指针将为char **words_temp = realloc(words, sizeof(char*) * number_of_words); if (words_temp) words = words_temp; else{ // Whoops, memory reallocation failure. }

使用此:

char **words;
words = malloc(9*sizeof(char *));
words[0] = malloc(strlen("the"));
strcpy(words[0],"the");

查看修订后的问题中的初始化:

words[0]

words有所谓的字符串&#39;&#39;,然后是以下行,复制&#39;&#39;&#39;显然,OP没有考虑到终止字符串字符或NUL,并且破坏了words[0] = malloc(strlen("the") + 1); strcpy(words[0], "the") 地址中包含的内存指针。

strdup

注意区别?或者为了理智,使用考虑的VOLUME ["/root/", "/var/log/"]函数,并添加额外的NUL字符。

答案 2 :(得分:0)

显然我没有重新分配足够的内存,这是我的工作代码:

    char **temp = realloc(words, (sizeof(char *) * number_of_words +1));