gpt4 book ai didi

c - 结构数组中的内存分配

转载 作者:太空宇宙 更新时间:2023-11-04 03:17:44 25 4
gpt4 key购买 nike

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*

编辑***我解决了这个问题。使用双指针不需要分配airport,但是airport的成员还是需要分配的。

//为结构体分配内存 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++;
}

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

最佳答案

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

关于c - 结构数组中的内存分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49822462/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com