【问题标题】:realloc not working for 2-D pointer arrayrealloc 不适用于二维指针数组
【发布时间】:2013-01-09 13:43:41
【问题描述】:

在下面的代码中,我要求用户给我一些字符串。我制作了一个二维指针 char 数组,以便使用指针字符串读取输入,该指针字符串指向长度为 50 的字符串的开头。我的问题是我在输入第一个字符串后一直崩溃.. 我假设我的问题与重新分配有关。我不习惯它..你能帮忙弄清楚发生了什么吗?我尝试使用 netbeans 进行调试,但没有看到任何有趣的东西,因为它没有为由 realloc 生成的新地址提供反馈!

代码如下:

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

int main()
{
    char *str,**string,buffer[50],temp[2];
    int i,j,q,size,counter;
    size=10;
    string=(char**) calloc(size,sizeof(char*));
    for (i=0; i<size; i++) string[i]=(char*) malloc(50*sizeof(char));
    printf("\nGimme strings, terminate input with x");
    i=0;
    gets(string[i]);
    temp[0]=120;//x
    temp[1]='\0';
    size=0;
    while(strcmp(string[i],temp)!=0)
    {
        string=realloc(string,size*sizeof(char**));
        i++;
        gets(string[i]);
        size++;
        counter++;
    }
return 0;
}

我想用这个 realloc 使指针表更大。

【问题讨论】:

  • strings[I] = realloc( strings[I], size * sizeof(char) );
  • 但我不想让字符串变大.. 我想让字符串表变大!
  • sun()/str/buffer/j/q(未使用)和单行for 循环之后,我不再试图找出代码。到目前为止,我会去帮助某人,而且质量如此低下的示例代码是浪费时间。
  • 哦,好的,您可以编辑您的问题以明确说明。
  • 1) 是的,我知道gets..还没有尝试过fgets。我会尽快。 2) 是的,很抱歉未使用的变量.. 这不是所有代码!!!!!!!!! 3)一行for循环有什么问题?

标签: c generics pointers realloc


【解决方案1】:
    string=realloc(string,size*sizeof(char**));
    i++;
    gets(string[i]);
    size++;

调用realloc 放大string 后,新部分不包含有效指针。因此,当您调用 gets 时,您传递给它的是一个您未能初始化的指针。

另外,size=0; 完全坏掉了。

【讨论】:

    【解决方案2】:

    realloc 不会用零初始化分配的内存,此外您忘记初始化新分配的字符串指针。

    考虑在 while 循环中向上移动 i++size++

    【讨论】:

      【解决方案3】:

      代码审查

      初始化所有变量

        char *str = NULL,**string = NULL,buffer[50] = {0},temp[2] = {0};
        int i = 0,j = 0,q = 0,size = 10,counter = 0;
      

      不要转换从 malloc/calloc 返回的内容,并尽可能使用 {} 以清楚起见

      string=calloc(size,sizeof(char*));
      for (i=0; i<size; i++) 
      { 
        string[i]=malloc(50*sizeof(char));
      }
      

      从键盘读取时不要使用gets,使用fgets(),因为您可以指定要读取的最大大小。

      printf("\nGimme strings, terminate input with x");
      char input[256];
      fgets(input,sizeof(input),stdin); // another varname, will explain below
      

      使用较新的编译器,您可以在需要的地方声明变量,而不是在函数顶部声明。

      char temp={'x','\0'}; // 120;//x
      

      这里设置 size=0 好像有点奇怪

      size=0;
      

      最好将用户输入的内容保存在单独的缓冲区(输入)中 然后如果它不是“x”,则将其复制到您的字符串数组中,而不是

      while(strcmp(string[i],temp)!=0)
      {
          string=realloc(string,size*sizeof(char**));
          i++;
          gets(string[i]);
          size++;
          counter++;
      }
      

      例如

      while (fgets(input,sizeof(input),stdin) != NULL && input[0] != 'x')
      {
         string[i] = calloc(1,strlen(input)+1); // add a byte for \0
         strncpy(string[i],input,strlen(input)-1); // not copying ending \n
         if ( ++i == size ) // a new chunk needed
         {
           char *newstring = realloc((size + 10)*sizeof(char*), string );
           if ( newstring != NULL )
           {
             string = newstring;
             size += 10;
           }
         }
      }
      

      【讨论】:

        猜你喜欢
        • 2021-12-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-17
        • 1970-01-01
        相关资源
        最近更新 更多