动态内存C中结构内指针数组的分配

时间:2015-03-16 00:52:26

标签: c

我有一个学校的任务,为生活游戏写一些功能,我一开始就被卡住了:

实现函数'createField',为包含游戏字段的'Field'结构分配所需的空间,并为参数'xsize'和'ysize'中给出的二维维数组分配。必须将字段中的每个单元格初始化为“DEAD”状态,以及结构中的“xsize”和“ysize”字段。

注意:测试假设字段的行(y轴)是数组的第一个维度(并且首先分配),而列是第二个维度。即,细胞被索引为[y] [x]。

您还需要实现函数'releaseField',以释放createField()分配的内存。测试将对所有内存版本使用此函数,因此未能实现此操作将导致Valgrind关于内存泄漏的错误

  typedef enum {
        DEAD,
        ALIVE
    } State;

    typedef struct {
        unsigned int xsize, ysize;
    State **cells;
     } Field;

第一项任务是编写创建字段并释放它的函数

Field *createField(unsigned int xsize, unsigned int ysize)
{
    (void) xsize; 
    (void) ysize;
    Field *create = malloc(sizeof( Field));
    create->xsize=xsize;
    create->ysize=ysize;
// for this part I don't know the syntax + I don understand what I am doing
    create->cells = malloc (ysize*sizeof(State*));
    for(unsigned int j=0;j<ysize;j++)
    {
        create->cells[j] = malloc(xsize*sizeof(State));
    }
    return create;   
}

我的清理功能:

void releaseField(Field *f)
{
    (void) f;
    for(unsigned int i=0;i<f->ysize;i++)
        free(f->cells[i]);
    free(f->cells);
    free (f);
}

我收到了valgrind错误:

==374== Conditional jump or move depends on uninitialised value(s)
==374==    at 0x40172C: test_createField (test_source.c:26)
==374==    by 0x4058A0: srunner_run_all (in /tmc/test/test)
==374==    by 0x402276: tmc_run_tests (tmc-check.c:122)
==374==    by 0x401F2F: main (test_source.c:225)
==374==  Uninitialised value was created by a heap allocation 
==374==    at 0x4C244E8: malloc (vg_replace_malloc.c:236) 
==374==    by 0x402960: createField (gameoflife.c:21) 
==374==    by 0x401662: test_createField (test_source.c:16) 
==374==    by 0x4058A0: srunner_run_all (in /tmc/test/test) 
==374==    by 0x402276: tmc_run_tests (tmc-check.c:122) 
==374==    by 0x401F2F: main (test_source.c:225) 
==374==  
==375== Conditional jump or move depends on uninitialised value(s) 
==375==    at 0x4017F5: countlive (test_source.c:42) 
==375==    by 0x401921: test_initField (test_source.c:62) 
==375==    by 0x4058A0: srunner_run_all (in /tmc/test/test) 
==375==    by 0x402276: tmc_run_tests (tmc-check.c:122) 
==375==    by 0x401F2F: main (test_source.c:225) 
==375==  Uninitialised value was created by a heap allocation 
==375==    at 0x4C244E8: malloc (vg_replace_malloc.c:236) 
==375==    by 0x402960: createField (gameoflife.c:21) 
==375==    by 0x4018F3: test_initField (test_source.c:57) 
==375==    by 0x4058A0: srunner_run_all (in /tmc/test/test) 
==375==    by 0x402276: tmc_run_tests (tmc-check.c:122) 
==375==    by 0x401F2F: main (test_source.c:225) 
==375==  
==375== Conditional jump or move depends on uninitialised value(s) 
==375==    at 0x401820: countlive (test_source.c:44) 
==375==    by 0x401921: test_initField (test_source.c:62) 
==375==    by 0x4058A0: srunner_run_all (in /tmc/test/test) 
==375==    by 0x402276: tmc_run_tests (tmc-check.c:122) 
==375==    by 0x401F2F: main (test_source.c:225) 
==375==  Uninitialised value was created by a heap allocation 
==375==    at 0x4C244E8: malloc (vg_replace_malloc.c:236) 
==375==    by 0x402960: createField (gameoflife.c:21) 
==375==    by 0x4018F3: test_initField (test_source.c:57) 
==375==    by 0x4058A0: srunner_run_all (in /tmc/test/test) 
==375==    by 0x402276: tmc_run_tests (tmc-check.c:122) 
==375==    by 0x401F2F: main (test_source.c:225) 
==375== 

请帮助我,我花了这么多时间搜索,但无法绕过它。

PS:我不能修改除函数内容之外的任何内容..

2 个答案:

答案 0 :(得分:3)

你没有做这部分。 “该字段中的每个单元格必须初始化为'DEAD'状态,以及结构中的'xsize'和'ysize'字段。”

malloc返回的数据未初始化。所以它可以被设置为任何东西(它不会默认为DEAD。)所以你需要将二维数组中的所有内容初始化为DEAD。


其他评论。您应该检查以确保malloc没有返回NULL。如果是这样,则意味着内存不足并希望处理错误而不是出现分段错误。

此外,您可以安全地删除(void) var;语句,因为它们可以使有关未使用变量的警告静音。但是,您目前正在使用它们,它们没有用处。

答案 1 :(得分:2)

作业说“场中的每个单元必须初始化为'DEAD'状态”。你不是那样做的。这意味着当其他一些代码访问一个单元格时,它正在读取一个垃圾值。

分配行后,需要循环并将每个单元格设置为DEAD。这样的事情应该有效:

create->cells[j] = malloc(xsize*sizeof(State)); // Your existing line
for(unsigned int i=0;i<xsize;i++)
  create->cells[j][i] = DEAD;
相关问题