[英]Dynamic Memory Allocation of array of pointers inside a structure in C
我的學校任務是為生活中的游戲編寫一些功能,而我一開始就陷入困境:
實現函數'createField',該函數為容納游戲場的'Field'結構以及參數'xsize'和'ysize'中給出的二維數組分配所需的空間。 必須將字段中的每個單元格以及結構中的“ xsize”和“ ysize”字段初始化為“ DEAD”狀態。
注意:這些測試假定字段的行(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:除了功能的內容,我無法修改其他任何內容。
您沒有做這部分。 “字段中的每個單元以及結構中的“ xsize”和“ ysize”字段都必須初始化為“ DEAD”狀態。”
從malloc返回的數據未初始化。 因此可以將其設置為任何值(默認不設置為DEAD。)因此,您需要將二維數組中的所有內容初始化為DEAD。
其他的建議。 您應該檢查以確保malloc
不返回NULL
。 如果這樣做,則意味着您內存不足,想處理該錯誤而不是遇到分段錯誤。
您也可以安全地刪除(void) var;
這些語句可以使有關未使用變量的警告消失。 但是,您當前正在使用它們,它們沒有任何作用。
作業顯示“字段中的每個單元必須初始化為'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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.