结构数组中的内存分配

时间:2018-04-13 17:39:16

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

MATCH (n:artist) RETURN  [(:tag { _id : 'jazz' })-[:TAGS]->(n) | n._id] AS ids

我正在尝试创建一个结构数组,但我无法弄清楚如何为结构的成员分配内存..它保持segfaulting。 LocID,fieldName和city都是char *

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

//为struct分配内存     airPdata **机场;

// buffer = malloc(sizeof(char)*(50 + 1));

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");

然而,当程序第二次回到while循环并且遇到// 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++; }

时,它会出现故障

2 个答案:

答案 0 :(得分:1)

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

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

数组内存airport[]
记忆分配&分配给airport[i]的每个元素( OP错过了这一部分。
记忆分配&分配给airport[i].LocID

等各种成员

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

// 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));

答案 1 :(得分:0)

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

你有一系列连续的机场

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

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

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

如果你想要A

,你的malloc会混合A和B.

DO

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

如果你想B做

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

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