6

我想为一个结构分配内存,该结构包含另一个名为table. 我检测到在最后将指针分配给函数时,linkedObjects数组中的变量会损坏,所以我认为我对动态内存的处理是错误的。

这就是我现在的做法:

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

编辑:我编辑了功能代码以反映答案:

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

但是当我到达最后的分配点时,table->linkedObjects[0].key那是空的,值是0x0get的溢出到一个值0x8048cc0。执行此行时会发生这种情况:

table->originalSize = tableSize;

另一个编辑:确认它在最后一次调用中随机发生(不仅在上面的行中):

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

3 回答 3

5

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

应该

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

我有时喜欢C。

`

于 2012-12-25T18:22:21.253 回答
1

像往常一样,摆脱在sizeof. 这就是您的内存分配应该看起来的样子

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

这也将解决第一次分配中的“错字”错误。

于 2012-12-25T18:34:43.267 回答
0
table->linkedObjects[i].next = malloc( MAX_IN_LIST*sizeof(Object) );
table->linkedObjects[i].top = malloc( MAX_IN_LIST*sizeof(Object) );

这似乎没有意义。当我看到nexttop使用集合时,我希望有一个指向一个的指针Object(指向集合中的下一项或集合中的第一项)。

您是否打算执行以下操作:

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

这将为每个 分配内存Object,然后设置指针。

于 2012-12-25T17:38:44.270 回答