创建结构对象并将其放入数组

时间:2018-10-25 15:29:43

标签: c memory-management struct

我正在尝试解决一个函数,该函数将创建一个新的struct对象,然后将其放入动态数组中。我尝试了多种变体,但是我一直遇到各种问题。这就是我现在正在使用的,但是遇到内存访问问题。

typedef struct  {
    int keynr;
    int access;
    time_t lastused;


} keycard;

void keyCreate(keycard *cardList, int keyid) {
    cardList[keyid].keynr = keyid + 100;
    cardList[keyid].access = 1;
    cardList[keyid].lastused = 0.0;
    }

int main () {
    keycard *cardList = 0;
    cardList = malloc(sizeof(keycard) * 1);
    keyCreate(&cardList, 0);
    printf("%d", cardList[0].access);

此代码给我:引发异常:读取访问冲突。 cardList为0x64。

我一直在阅读很多有关指针和内存分配的信息,但是显然我缺少一些东西。

2 个答案:

答案 0 :(得分:1)

如果要向阵列中动态添加新卡,则需要将其包装在另一个数据结构中:

typedef struct  
{
    int keynr;
    int access;
    time_t lastused;
} keycard;

typedef struct 
{
    keycard *keyarray;
    size_t size;
}keystorage;

int keyCreate(keystorage *cardList, size_t keyid) 
{
    if (cardList -> keyarray == NULL || keyid + 1 > cardList -> size)
    {
        keycard *new = realloc(cardList -> keyarray, sizeof(*(cardList -> keyarray)) * (keyid + 1));

        if(!new) return -1;   //error
        cardList -> keyarray = new;
        cardList -> size = keyid + 1;
    }

    cardList -> keyarray[keyid].keynr = keyid + 100;
    cardList -> keyarray[keyid].access = 1;
    cardList -> keyarray[keyid].lastused = 0.0;
    return 0; //OK
}




int main (void) {
    keycard key;
    keystorage cards = {NULL, 0};

    keyCreate(&cards, 500);
    printf("%d", cards.keyarray[500].access);

    return 0;
}

答案 1 :(得分:0)

您将错误的类型传递给keyCreate。该函数需要一个指向keycard的指针,但是您正在向它传递一个双指针。 &的意思是“使用地址”,这将cardList变成keyCard**类型。相反,请考虑以下因素:

void keyCreate(keycard *cardList, int keyid) {
    cardList[keyid].keynr = keyid + 100;
    cardList[keyid].access = 1;
    cardList[keyid].lastused = 0;  // time_t is most likely a signed integer
}

int main (void) {
    keycard *cardList = malloc(sizeof(keycard) * 1);
    // always check if malloc succeeds, and if it does not, handle the error somehow
    if (cardList == NULL)
    {
      fprintf(stderr, "Insufficient mem\n");
      return -1;
    }

    keyCreate(cardList, 0);
    printf("%d\n", cardList[0].access);  // the \n will flush the output and
                                         // put each item on its own line

    // cleanup when you're done, but the OS will do this for you when the
    // process exits also
    free(keyCreate);

    return 0;
}

此外,time_t最有可能是一个有符号整数(What is ultimately a time_t typedef to?),因此将其分配给0.0可能不正确,但是您需要检查typedef到在您的系统上。

最后,我认为这只是一个MCVE,但在这种情况下,建议不要使用mallocmalloc的两个主要原因是,在运行时您不知道需要多少数据,或者您需要“大量”数据。在这种情况下,这些都不是正确的。根据您的介绍,我可能会执行以下操作:

#define NUM_KEY_CARDS 1

void keyCreate(keycard *cardList, int keyid) {
        cardList[keyid].keynr = keyid + 100;
        cardList[keyid].access = 1;
        cardList[keyid].lastused = 0;  // time_t is most likely a signed integer
    }

int main (void) {
    keycard cardList[NUM_KEY_CARDS];

    for (int keyid=0; keyid<NUM_KEY_CARDS; keyid++)
    {
        keyCreate(cardList+keyid, keyid);
        // or keyCreate(&(cardList[keyid]), keyid);
        printf("%d\n", cardList[keyid].access);
    }

    return 0;
}