【问题标题】:Writing a function to dynamically allocate memory in C在 C 中编写一个动态分配内存的函数
【发布时间】:2020-02-05 23:00:11
【问题描述】:

我目前正在进行的项目需要四个动态创建的字符串数组(每个字符串不超过 50 个字符)。因此,我正在尝试编写一个函数,该函数接受一个指向指针的指针并为该变量动态分配内存。

据我所知:

void makeArray(char*** arr[], int n) {
  int i;
  *arr = malloc(n*sizeof(char*));
  for (i = 0; i<n; i++) {
    *arr[i] = malloc(sizeof(char)*50);
  }
}

int main() {

  char** test;

  makeArray(&test,4);

  return 0;
}

当我编译和运行时,我得到这个错误:

main.c:16:13: warning: passing argument 1 of ‘makeArray’ from incompatible pointer type [-Wincompatible-pointer-types]
   makeArray(&test,4);
             ^
main.c:4:6: note: expected ‘char ****’ but argument is of type ‘char ***’
 void makeArray(char*** arr[], int n) {

当我使用 C Tutor 时,该函数似乎成功地接收了我的 test 数组并分配了 4 指针槽。然后它成功地将 50 个字符分配给 0th test 插槽。但是,当循环再次运行时,我得到一个错误。

我已经被困在这两天了,所以我欢迎 Stack Overflow 的好心用户提出任何建议!

【问题讨论】:

  • 天啊,三星级程序员!问题:为什么函数返回void?
  • 当我看到char*** x[] 时,我看到了四颗星。这是一个荒谬的指针主义水平。尽量保持在最多两个,如果绝对必要的话,三个。
  • 更糟糕的是,char ***arr[][ 超出了我的掌握范围。
  • 该函数只是在原地修改了数组,所以不需要返回任何东西。我偷了here的***。
  • @chadathin 阅读这个问题和答案:Correctly allocating multi-dimensional arrays 当您在 malloc() 周围调用嵌套循环来创建“数组”时,它将有助于解释您实际在做什么。当你这样做时,你并没有真正创建多维数组。您实际上是在创建指向一维值数组的指针数组的指针数组。对于大型 n 维“数组”,嵌套分配也可能非常慢。

标签: c arrays pointers


【解决方案1】:
#include <stdio.h>
#include <stdlib.h>

void makeArray(char*** arr, int n) {
  int i;
  *arr = calloc(n, sizeof (char*));
  for (i = 0; i<n; i++) 
  {
    (*arr)[i] = calloc(50, sizeof(char));
  }
}

int main() {

  char **test;

  makeArray(&test,4);

  return 0;
}

应该是你要找的。它是如何工作的:

  • 首先,我将您的原型更改为“char ***”作为“char ***arr[]* asactally char****”
  • 第二个 *arr(允许在 main 中写入变量测试)
  • 第三个有点棘手 (*arr) 写入 test 和 [] 使用 test 作为数组。

我使用 calloc 而不是 malloc,因为从我的角度来看,它看起来比 malloc 更好的数组,它确保你的字符串初始化为 0(零终止)

【讨论】:

  • 这就是票!非常感谢您帮助我了解您在此过程中的修改!实际上,我们的讲师对malloc 进行了大量的报道,只是对calloc 有点“呆滞”,所以我必须查看手册页。再次感谢您!
【解决方案2】:

在这里,我从 Schafwolle(一个非常柔和流畅的昵称!)中获取了解决方案,并混合了我以前的解决方案和我返回分配数组的想法。 不要忘记按相反的顺序释放内存。

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

#define MAX_STRING_SIZE 50

char** makeArray(int n) {
  int i;
  char** arr = calloc(n, sizeof (char*));
  for (i = 0; i<n; i++) 
  {
    arr[i] = calloc(MAX_STRING_SIZE, sizeof(char));
  }
  return arr;
}

void recycleArray(char** arr, int n) {
  int i;
  for (i = 0; i<n; i++) 
  {
    free(arr[i]);
  }
  free(arr);
}

int main() {

  char** test = makeArray(4);
  strncpy(test[0],"Have ", MAX_STRING_SIZE-1);
  strncpy(test[1],"a ", MAX_STRING_SIZE-1);
  strncpy(test[2],"nice ", MAX_STRING_SIZE-1);
  strncpy(test[3],"evening.", MAX_STRING_SIZE-1);
  printf("%s%s%s%s\n", test[0], test[1], test[2], test[3]);
  recycleArray(test, 4);
  return 0;
}

【讨论】:

  • 哦,哇。这很棒!我什至从没想过只返回 char** 并将其分配给指针。谢谢!
【解决方案3】:

从这里开始:

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

#define MAX_STRING_SIZE 50

void makeArray(char* arr[], int n) {
  int i;
  for (i = 0; i<n; i++) {
    arr[i] = malloc(sizeof(char)*MAX_STRING_SIZE);
  }
}

int main() {

  char* test[4];
  makeArray(test,4);

  strncpy(test[0],"Have ", MAX_STRING_SIZE-1);
  strncpy(test[1],"a ", MAX_STRING_SIZE-1);
  strncpy(test[2],"nice ", MAX_STRING_SIZE-1);
  strncpy(test[3],"evening.", MAX_STRING_SIZE-1);
  printf("%s%s%s%s\n", test[0], test[1], test[2], test[3]);

  return 0;
}

如果您还想动态创建指针数组,只需返回指针而不是 void。这使它更具可读性...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-31
    • 2010-12-28
    • 1970-01-01
    • 2023-03-17
    • 2017-04-19
    • 1970-01-01
    相关资源
    最近更新 更多