【问题标题】:Memory Allocation in an array of structures结构数组中的内存分配
【发布时间】:2018-09-24 03:16:04
【问题描述】:
airPdata **airport = malloc(sizeof(airport) * (50+1));
    printf("Passes airPdata **airport\n");
//  buffer = malloc(sizeof(char) * (50+1));

// puts the strings into char line
while(fgets(line, 1024, fp) != NULL)
{
    // has pointer value point to line
    value = line;
    printf("Before creating space for struct members\n");
    // creating space for the struct members
    airport[j]->LocID = malloc(sizeof(char)*(50+1));

    airport[j]->fieldName = malloc(sizeof(char)*(50+1));

    airport[j]->city = malloc(sizeof(char)*(50+1));

    printf("after\n");

我正在尝试创建一个结构数组,但我只是不知道如何为结构的成员分配内存。它不断出现段错误。 LocID、fieldName和city都是char*

编辑*** 我解决了这个问题。使用双指针不需要分配机场,但机场的成员仍然需要分配。

// 为结构分配内存 airPdata **机场;

// 缓冲区 = malloc(sizeof(char) * (50+1));

// puts the strings into char line
while(fgets(line, 1024, fp) != NULL)
{
    // has pointer value point to line
    value = line;
printf("Yes\n");
    // creating space for the struct members
    airport[j]->LocID = malloc(sizeof(char)*(50+1));    
    airport[j]->fieldName = malloc(sizeof(char)*(50+1));
    airport[j]->city = malloc(sizeof(char)*(50+1));
    j++;
 }

但是,当程序第二次返回 while 循环并遇到 airport[j]->LocID = malloc 时,程序 seg 出现错误

【问题讨论】:

  • airPdata *airport = malloc(sizeof(*airport) * (50+1)); 不是**
  • 我原来用的是airPdata airport[MAX];因为我想使用一个结构数组,但是把它改成了双指针
  • airPdata *airport[MAX] 是指针数组,而不是结构数组
  • 下定决心——airport 可以是一个指针数组,你先分配然后再分配每个元素;或者它可以只是一个扁平的结构数组,分配一次(然后使用 airport[i].city 而不是 ->)
  • 我想使用指针数组,而不是结构,抱歉

标签: c memory-management struct dynamic-memory-allocation


【解决方案1】:

OP 的代码最大的失败是没有为每个airport[i] 分配内存


使用airPdata **airportI want to use an array of pointers,代码需要分配2 个级别并使用数组

数组内存airport[]
分配和分配给airport[i] 的每个元素的内存(OP 错过了这部分。
分配和分配给各个成员的内存,例如airport[i].LocID


数组airport 的内存很简单,如下所示。 airPdata **airport 是指针而不是数组。而是使用 array,因为这是既定的设计目标。

// define array element count. 
#define AIRPORT_N 100 
// Declare the array.
airPdata *airport[AIRPORT_N];
// Keep tack of how much of the array is used.
size_t n = 0;

现在分配、读取并开始填充数组,根据需要进行分配。

#define AIRPORT_STRING_SIZE (50 + 1)
char line[1024];
while(n < AIRPORT_N && fgets(line, sizeof line, fp)) {
  // Allocate memory for one element of `airport`
  // Notice no cast nor type coded here.
  airport[n] = malloc(sizeof *(airport[n]));
  if (airport[n] == NULL) {
    // Something simple for now.
    fprintf(stderr, "OOM\n");
    break;
  }
  // Create space for each string,  
  // TODO: add check for Out-of-Memory
  airport[n]->LocID = malloc(AIRPORT_STRING_SIZE);
  airport[n]->fieldName = malloc(AIRPORT_STRING_SIZE);
  airport[n]->city = malloc(AIRPORT_STRING_SIZE);

  // Code to parse `line` into `airport[n]` members.

  // Usually the parsing happens first and if successful, the above allocations occur.

  // If the `LocID` string (and others) need not change then 
  //   use below to allocate a right-sized memory
  //   after parsing instead of allocating to some max size, like above.
  airport[n]->LocID = strdup(LocID_string);

  n++;
}

稍后全部释放

for (size_t i = 0; i < n; i++) {
  free(airport[i]->LocID);
  free(airport[i]->fieldName);
  free(airport[i]->city);
  free(airport[i]);
}

详细信息:请注意以下细微错误。它分配给airport的大小,即airPdata **类型。

相反,它应该分配给* airport的大小,即airPdata *类型。

很常见的是,所有类型的对象指针都具有相同的大小,但这种相同性并未在 C 中的所有类型中指定。

最好分配给类型的取消引用指针的大小。它更有可能编码正确,更易于查看和维护。

// airPdata **airport = malloc(sizeof(airport) * (50+1));
airPdata **airport = malloc(sizeof *airport * (50+1));

【讨论】:

  • 谢谢,我不知道我还必须 malloc 个别机场[j]。
【解决方案2】:

您正在混合使用两种方法。

您有一系列连续的机场

airports -----> ap1 | ap2 | ap3

B 你有一个指向机场的指针数组(在内存中不一定彼此相邻

airports --> aptr1 | aptr2 | aptr3
               |        |        |
               v        v        v
              ap1       ap2     ap3

你的 malloc 混合了 A 和 B。如果你想要 A

airPdata *airport = malloc(sizeof(airport) * (50+1));

如果你想要 B 做

airPdata **airport = malloc(sizeof(airport*) * (50+1));

然后你将不得不为每个指针槽分配一个机场对象。

【讨论】:

  • 他确实做到了,但他的代码是两者的混合。我希望他确定他的目标是他的意图
猜你喜欢
  • 1970-01-01
  • 2014-04-21
  • 2021-05-29
  • 1970-01-01
  • 2013-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多