【问题标题】:C: Realloc behaves in a way i cant figure out whyC:Realloc 的行为方式我不知道为什么
【发布时间】:2016-04-30 08:34:00
【问题描述】:
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv[]){

    char buffer[103];
    char **words = malloc(1 * sizeof(*words));
    size_t counter = 0;
    size_t array_size = 2;


    for(int i = 0; i < 5; i++){
        if(!fgets(buffer, 103, stdin)){
            fputs("fgets failed", stderr);
        }
        words[counter] = buffer;
        char **more_words = realloc(words, array_size * sizeof(*more_words));
        words = more_words;
        array_size++;
        counter ++;
    }
    printf("********************************************************");


    for(int i = 0; i < 5; i++){
        printf("%s\n", words[i]);
    }


}

现在这是我正在处理的简化代码。 我知道我不会处理很多可能发生的错误。

关键是,当您执行此操作时,单词数组似乎有 5 个“最后一个”条目。

假设你给 fgets :

1
2
3
4
5

,然后

words[0] = 5;
words[1] = 5;
words[2] = 5;
words[3] = 5;
words[4] = 5;

为什么不是:

words[0] = 1;
words[1] = 2;
words[2] = 3;
words[3] = 4;
words[4] = 5;

?

【问题讨论】:

    标签: c pointers malloc realloc


    【解决方案1】:

    问题不在于realloc,而在于您分配给分配的指针的内容:

    words[counter] = buffer;
    

    buffer 始终是同一个指针,所以最后一个字符串读入缓冲区。

    您需要malloc 并为每一行复制缓冲区:

    words[counter] = malloc(strlen(buffer)+1);
    strcpy(words[counter], buffer);
    

    不用说,您应该在NULL-检查realloc 返回的值,然后再将其分配回words

    【讨论】:

      【解决方案2】:
      if(!fgets(buffer, 103, stdin)){
              fputs("fgets failed", stderr);
      }
      words[counter] = buffer;
      

      每次调用fgets 时都会覆盖一个缓冲区,因此words 中的所有字符串都有效地指向同一个字符数组。试试这个:

      if(!fgets(buffer, 103, stdin)){
              fputs("fgets failed", stderr);
      }
      // here make a new buffer and copy the string just read into it.
      char *new_buffer = malloc(strlen(buffer) + 1);
      strcpy(new_buffer, buffer);
      words[counter] = new_buffer;
      

      【讨论】:

        猜你喜欢
        • 2016-09-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-08-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-04
        相关资源
        最近更新 更多