struct 内部的 malloc 结构数组

Posted

技术标签:

【中文标题】struct 内部的 malloc 结构数组【英文标题】:malloc array of struct inside of struct 【发布时间】:2022-01-07 04:48:54 【问题描述】:

我在学校有一个项目,需要制作一个包含机场数量和 Airport 数组(另一个结构)的 AirportManager 结构。我开始编写代码,但我遇到了机场数组的 malloc 问题。 到目前为止,我附加了我编写的代码,我遇到的问题是这些值没有保存在 AirportManager 的 airportArray 中。

//AirportManger Struct
typedef struct 
    Airport* airportArray;
    int airportAmount;
AirportManager;

void initAirportManager(AirportManager* airportManager) 

    airportManager->airportAmount = 0;
    airportManager->airportArray = (AirportManager*)malloc(0);

void addAirport(AirportManager* airportManager)

    Airport airport;
    printf("Enter Airport Name: ");
    scanf("%s", airport.airportName);
    printf("Enter Airport Address: ");
    scanf("%s", airport.airportAddress);
    
    airportManager->airportAmount++;
    airportManager->airportArray = (Airport*)realloc(airportManager->airportArray, airportManager->airportAmount * sizeof(Airport));
    airportManager->airportArray = airport;



//Airport Struct
typedef struct 
    char airportName[MAX];
    char airportAddress[MAX];
Airport;

//Main
AirportManager airportManager;
initAirportManager(airportManager);
addAirport(&airportManager);

【问题讨论】:

airportManager->airportArray = realloc(...); 后跟(无效)赋值 airportManager->airportArray = airport。如果最后一个任务是有效的,你认为会发生什么?如果你有例如int a; a = 10; a = 20; a 的值是多少?为什么在指针方面会有所不同? 考虑到无效分配和其他问题,您的代码不是正确的minimal reproducible example,因为它甚至无法构建。始终测试您向我们展示的minimal reproducible example,以确保它复制了您提出的问题并且没有任何不相关的问题。 您是否介意edit 提出您的问题并提供一些详细信息您有什么问题,好吗? -- 在任何情况下,您都需要检查realloc() 的结果,但我不认为这是导致崩溃或您得到任何结果的原因。 -- 正如 Someprogrammerdude 已经说过的,将结构分配给指向结构的指针是错误的。将编译器的警告级别提高到最大并更正代码,直到不再输出诊断信息。您需要重新考虑要分配的内容。 【参考方案1】:

代码有一些问题。我们不应该:

malloc(0)分配零字节 两次分配给airportManager->airportArray 使用scanf

这里是修改的代码。它使用mallocrealloc 更好,fgets 而不是scanf

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

#define SIZ 512

typedef struct 
    char airportName[SIZ];
    char airportAddress[SIZ];
 Airport;

typedef struct 
    Airport* airportArray;
    int airportAmount;
 AirportManager;

// Call first on AirportManager
void initAirportManager(AirportManager* airportManager)

    airportManager->airportAmount = 0;
    airportManager->airportArray = NULL;


// Call last on AirportManager
void finalAirportManager(AirportManager* airportManager)

    airportManager->airportAmount = 0;
    if (airportManager->airportArray != NULL)
        free(airportManager->airportArray);
    airportManager->airportArray == NULL;


// Add an airport to the manager
void addAirportByNameAndAddress(AirportManager* airportManager, char *name, char *address)

    // Calculate the amount of memory needed
    size_t mem = (airportManager->airportAmount + 1) * sizeof(Airport);

    // Get the memory required
    Airport* newAirports = NULL;
    if (airportManager->airportArray == NULL)
        newAirports = (Airport*)malloc(mem);
    else
        newAirports = (Airport*)realloc(airportManager->airportArray, mem);
    if (newAirports == NULL)
    
        // error: out of memory
        return;
    

    // Copy the name and the address to new the new Airport
    Airport *current = newAirports + airportManager->airportAmount;
    memset(current->airportName, '\0', SIZ);
    strncpy(current->airportName, name, SIZ - 1);
    memset(current->airportAddress, '\0', SIZ);
    strncpy(current->airportAddress, address, SIZ - 1);

    // Update the manager
    airportManager->airportAmount++;
    airportManager->airportArray = newAirports;


void addAirport(AirportManager* airportManager)

    char name[SIZ] =  0 ;
    char address[SIZ] =  0 ;
    printf("Enter Airport Name: ");
    fgets(name, SIZ - 1, stdin);
    printf("Enter Airport Address: ");
    fgets(address, SIZ - 1, stdin);
    addAirportByNameAndAddress(airportManager, name, address);


void main() 
    AirportManager airportManager;
    initAirportManager(&airportManager);
    addAirport(&airportManager);
    finalAirportManager(&airportManager);

【讨论】:

首先,如果我想感谢您的帮助,我复制了代码并在没有 finalAirportManager 函数的情况下运行它,以查看我输入的值是否保存在数组中,但在调试模式下已插入它说“错误读取字符串字符”的值。你知道问题可能是什么吗? 如果这个例子用malloc解决了你的第一个问题,但你还有另一个问题,那么再问一个问题。可能包括调试过程的屏幕截图。

以上是关于struct 内部的 malloc 结构数组的主要内容,如果未能解决你的问题,请参考以下文章

malloc struct vs. 的“成员”当结构非常简单时,整个结构

C Struct typdef malloc 赋值

结构体和双链表 struct myStruct int ID; int TYPE; char NAME[20]; ;

如何在pyspark /中的结构内爆炸结构中的内部数组

函数内部结构失效的 Malloc

基于struct内部的元素对指向struct的数组进行排序