简体   繁体   English

c - 为包含另一个struct数组的struct正确分配内存

[英]c - properly allocate memory for a struct containing an array of another struct

I want to allocate memory for a struct that contains an array of another struct named table . 我想为包含另一个名为table结构的数组的结构分配内存。 I detected that when assigning the pointers to the functions at the end, the variables in the linkedObjects array get corrupted so i think my handling of dynamic memory is wrong. 我检测到在指向末尾函数的指针时, linkedObjects数组中的变量被破坏,所以我认为我对动态内存的处理是错误的。

This is how i'm doing it now: 这就是我现在这样做的方式:

typedef struct Object {
    void *key;
    struct Object *top;
    struct Object *next;
} Object;

typedef struct Table{
    Object *linkedObjects;
    size_t size, originalSize;
    HashFcn hfun;
    PrintFcn pfun;
    ComparisonFcn fcomp;
} Table;

TableP CreateTable(size_t tableSize, HashFcn hfun, PrintFcn pfun, ComparisonFcn fcomp)
{
    int i;
    struct Table *table = malloc(sizeof(table));
    if (table==NULL)
    {
        ReportError(MEM_OUT);
        return NULL;
    }
    table->linkedObjects = NULL;
    table->linkedObjects  = malloc(tableSize * sizeof(Object));

    for(i=0;i<tableSize;i++)
    {

        table->linkedObjects[i].next = malloc( MAX_IN_LIST*sizeof(Object) );
        table->linkedObjects[i].top = malloc( MAX_IN_LIST*sizeof(Object) );
        table->linkedObjects[i].key = NULL;
        table->linkedObjects[i].top->key = NULL;
        table->linkedObjects[i].next->key = NULL;

        if (table->linkedObjects[i].next == NULL)
        {
            ReportError(MEM_OUT);
            return NULL;
        }
    }

    table->size = tableSize;
    table->originalSize = tableSize;
    table->hfun = hfun;
    table->pfun = pfun;
    table->fcomp = fcomp;
    return table;
}

Edit: I edited the function code to reflect the answers: 编辑:我编辑了功能代码以反映答案:

TableP CreateTable(size_t tableSize, HashFcn hfun, PrintFcn pfun, ComparisonFcn fcomp)
{
    int i;
    struct Table *table = malloc(sizeof(table));
    if (table==NULL)
    {
        ReportError(MEM_OUT);
        return NULL;
    }
    table->linkedObjects = NULL;
    table->linkedObjects  = malloc(tableSize * sizeof(Object));

    if (table->linkedObjects == NULL)
    {
        ReportError(MEM_OUT);
        return NULL;
    }

    for(i=0;i<tableSize;i++)
    {
        table->linkedObjects[i].next = NULL;
        table->linkedObjects[i].top = NULL;
        table->linkedObjects[i].key = NULL;
    }

    table->size = tableSize;
    table->originalSize = tableSize;
    table->hfun = hfun;
    table->pfun = pfun;
    table->fcomp = fcomp;
    //printf("%p\n", table->hfun);
    return table;
}

but still when i get to the point of the assignments at the end, the table->linkedObjects[0].key that is null and value is 0x0 get's overrun to a value 0x8048cc0 . 但是当我到达末尾的赋值点时, table->linkedObjects[0].key为null且值为0x0 get的溢出为值0x8048cc0 This occurs when this line is executed: 执行此行时会发生这种情况:

table->originalSize = tableSize;

Another Edit: Confirmed that it happens randomly in the last calls (not only in the line above): 另一个编辑:确认它在最后一次调用中随机发生(不仅在上面的行中):

table->size = tableSize;
table->originalSize = tableSize;
table->hfun = hfun;
table->pfun = pfun;
table->fcomp = fcomp;

struct Table *table = malloc(sizeof(table));

should be 应该

struct Table *table = malloc(sizeof(Table));

I sometimes love C. 我有时喜欢C.

` `

As usual, get rid of the habit of using type names under sizeof . 像往常一样,摆脱在sizeof下使用类型名称的习惯。 This is how your memory allocations should have looked 这就是你的内存分配应该如何看

Table *table = malloc(sizeof *table);
...
table->linkedObjects = malloc(tableSize * sizeof *table->linkedObjects);

That would also fix the "typo" error in the first allocation. 这也将修复第一次分配中的“拼写错误”错误。

table->linkedObjects[i].next = malloc( MAX_IN_LIST*sizeof(Object) );
table->linkedObjects[i].top = malloc( MAX_IN_LIST*sizeof(Object) );

This doesn't seem to make sense. 这似乎没有意义。 When I see next or top with collections, I expect a single pointer to one Object (to the next item in the collection or to the first item in the collection). 当我看到nexttop with collections时,我希望有一个指向一个Object指针(指向集合中的下一个项目或集合中的第一个项目)。

Did you mean to do the following: 你的意思是做以下事情:

for(i=0;i < (tableSize-1);i++)
{
    table->linkedObjects[i].top = table->linkedObjects[0];
    table->linkedObjects[i].next = table->linkedObjects[i+1];
    table->linkedObjects[i].key = NULL;
}

This will allocate memory for each Object , and sets up the pointers afterwards. 这将为每个Object分配内存,然后设置指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM