【问题标题】:C Programming: malloc() inside a functionC 编程:函数内部的 malloc()
【发布时间】:2019-08-07 08:06:51
【问题描述】:

我想在函数中分配内存,然后在 main() 中使用该空间。
函数中一切正常,但我永远无法访问 main() 中的数据。
有两条内存分配指令用于模拟二维字符数组

这是我的代码:

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

int getNetworks(char **networks) {

  networks = malloc(5 * sizeof(char*));
  printf("networks in function:%p\n", networks);

  networks[0] = (char*) malloc(4);
  strcpy(networks[0], "aaa");
  networks[1] = (char*) malloc(3);
  strcpy(networks[1], "bb");
  networks[2] = (char*) malloc(5);
  strcpy(networks[2], "cccc");
  networks[3] = (char*) malloc(6);
  strcpy(networks[3], "ddddd");
  networks[4] = (char*) malloc(2);
  strcpy(networks[4], "e");
  return 5;
}

int main ()
{
  char **networks = NULL;
  int nbNetworks;

  printf("networks before call:%p\n", networks);
  nbNetworks = getNetworks(&*networks);
  printf("networks after call:%p\n", networks);
  for (int i=0; i<=nbNetworks-1; i++) {
    printf ("%s\n", networks[i]);
  }
  return 0;
}

输出是

networks before call:0x0
networks in function:0x7feb65c02af0
networks after call:0x0
Segmentation fault: 11

【问题讨论】:

  • 那应该是*networks = malloc(5 * sizeof(char*)); 因为你要修改父级的变量,而不是栈上的value参数。
  • getNetworks(&*networks);闻起来很臭。
  • 在处理父数组时,它应该是(*networks)[0] = (char*) malloc(4);

标签: c arrays function pointers malloc


【解决方案1】:

以下需要修复:

int getNetworks(char ***networks)

因为它是指向字符指针数组的指针。 (您也可以写成
char **networks[])。

在函数中,您必须首先取消引用指针才能处理父变量:

    *networks = malloc(5 * sizeof(char*));
    (*networks)[0] = (char*) malloc(4);

在 main 中,您的声明是正确的,但是,您必须调用为:

    getNetworks(&networks);

所以函数可以作用于父变量。

【讨论】:

    【解决方案2】:

    你的代码有很多问题,我就不评论了,建议你这个解决方案:

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    char** getNetworks(int *n)
    {
      int count = 5;
      char ** networks = malloc(count * sizeof(char*));
      printf("networks in function:%p\n", networks);
    
      networks[0] = (char*) malloc(4);
      strcpy(networks[0], "aaa");
      networks[1] = (char*) malloc(3);
      strcpy(networks[1], "bb");
      networks[2] = (char*) malloc(5);
      strcpy(networks[2], "cccc");
      networks[3] = (char*) malloc(6);
      strcpy(networks[3], "ddddd");
      networks[4] = (char*) malloc(2);
      strcpy(networks[4], "e");
      *n = count;
      return networks;
    }
    
    int main ()
    {
      char **networks = NULL;
      int nbNetworks;
    
      printf("networks before call:%p\n", networks);
      networks = getNetworks(&nbNetworks);
      printf("networks after call:%p\n", networks);
      for (int i=0; i < nbNetworks; i++) {
        printf ("%s\n", networks[i]);
      }
      return 0;
    }
    

    【讨论】:

    • 警告:malloc(*n * sizeof(char*)) *n(实际上是 nbNetworks)未初始化,移动 *n = 5; 之前(我不是 DV ;-))跨度>
    • @bruno 谢谢,已更正。我没有执行它,这是一个错误。
    • 它有效。在“int count = 5”之后只缺少一个分号。很好的解决方案。
    • @Noury 好的,很高兴。我直接在编辑器中编写代码,所以存在一些错误。
    猜你喜欢
    • 2015-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-05
    • 2017-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多