Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用realloc在C中进行动态内存分配_C_Memory_Dynamic_Allocation - Fatal编程技术网

使用realloc在C中进行动态内存分配

使用realloc在C中进行动态内存分配,c,memory,dynamic,allocation,C,Memory,Dynamic,Allocation,我已经阅读了关于使用realloc获取指向更大内存地址空间开头的新指针的另一个SO问题,但我无法找出我做错了什么。它打印回溯和内存转储。我后来尝试访问strhldr,但我认为它甚至没有走那么远 char *strhldr = (char *)malloc(strsize); int chrctr = 0; if(chrctr == strsize - 3){ // when you get close strsize = strsize*2; //double size char

我已经阅读了关于使用realloc获取指向更大内存地址空间开头的新指针的另一个SO问题,但我无法找出我做错了什么。它打印回溯和内存转储。我后来尝试访问strhldr,但我认为它甚至没有走那么远

char *strhldr = (char *)malloc(strsize);

 int chrctr = 0;
 if(chrctr == strsize - 3){ // when you get close
  strsize = strsize*2; //double size
  char *temp = realloc(strhldr, strsize); //make more room
    if(temp == NULL)
     printf("reallocate failed\n");
    else{
     strhldr = temp;
     free(temp); // removed same issue
    }
} 

// Later attempt to add it to an array
cmdargs[i] =  strhldr;
这都是在while循环中,chrctr和strsize会递增

完整代码

  int argctr = 64;
  char **cmdargs = (char **) malloc(argctr * sizeof(char*));
  char c = getchar();
  int i = 0;


  while(c != '\n' && c != EOF){ //read through a String of stdin
  int strsize = 32;
  char *strhldr = (char *)malloc(strsize);
  char *strstarthldr = strhldr;


    if(c == ' ')
      c = getchar();
    while(c != ' ' && c != '\n' && c != EOF){

      int chrctr = 0;
     if(chrctr == strsize - 3){ // when you get close
      strsize = strsize*2; //double size
      char *temp = realloc(strhldr, strsize); //make more room
        if(temp == NULL)
         printf("reallocate failed\n");
        else
         strhldr = temp;

    }      //add that word to the array of strings
      strhldr[chrctr] = c;
      chrctr++;
      c = getchar();

    }
    strhldr[charctr] = '\0';
    //strhldr = strstarthldr;
    cmdargs[i] =  strhldr;
    i++;
  }

一旦成功,realloc将在需要时释放其论点。因此,删除对free(temp)的调用。

如果成功,realloc将在需要时释放其参数。所以取消对免费(temp)的调用。

我不太清楚你想做什么,但我相信
免费(temp)使strhldr无效,未来对它的读/写访问将给您带来麻烦。

我不太清楚您想做什么,但我相信
免费(temp)使strhldr无效,将来对它的读/写访问将给您带来麻烦。

第二个问题-您的值charctr(非chrctr)未设置。这是您的循环的一个版本。我还没有测试过,但应该很接近

  if(c == ' ') {
      c = getchar();
  }

  int chrctr = 0;
  while(c != ' ' && c != '\n' && c != EOF){
     if(chrctr == strsize - 3){ // when you get close
        strsize = strsize*2; //double size
        char *temp = realloc(strhldr, strsize); //make more room
        if(temp == NULL) {
           printf("reallocate failed\n");
           break;
        }
        else {
           strhldr = temp;
        }
     } 
     strhldr[chrctr] = c;
     chrctr++;
     c = getchar();
  }
  strhldr[chrctr] = 0;

第二个问题-您的值charctr(非chrctr)未设置。这是您的循环的一个版本。我还没有测试过,但应该很接近

  if(c == ' ') {
      c = getchar();
  }

  int chrctr = 0;
  while(c != ' ' && c != '\n' && c != EOF){
     if(chrctr == strsize - 3){ // when you get close
        strsize = strsize*2; //double size
        char *temp = realloc(strhldr, strsize); //make more room
        if(temp == NULL) {
           printf("reallocate failed\n");
           break;
        }
        else {
           strhldr = temp;
        }
     } 
     strhldr[chrctr] = c;
     chrctr++;
     c = getchar();
  }
  strhldr[chrctr] = 0;

strhldr在哪里定义和设置?它是否打印过“重新分配失败”?它打印的是内存转储而不是“重新分配失败”?您确定malloc成功了吗?如果你的malloc返回NULL…你能用-g3编译它并在它退出时显示堆栈吗?strhldr在哪里定义和设置?它是否打印过“重新分配失败”?它打印内存转储而不是“重新分配失败”?你确定你的malloc成功了吗?如果您的malloc返回NULL…您能用-g3编译它并在它退出时向我们显示堆栈吗?