c - Valgrind invalid write, after struct code addition -


for school have initialize map cells. while analyzing code valgrind, ran problems. initialize function used worked previously, stopped functioning after adding 2 additional interger pointers , integer. memory allocated dynamically, , bug shows in part there, has got me stumped. error occured following:

==4877== invalid write of size 4 ==4877==    @ 0x401723: initialize_map (list.c:312) ==4877==    0x400b99: main (initialization.c:14) ==4877==  address 0x550deb8 0 bytes after block of size 2,584 alloc'd ==4877==    @ 0x4c2db8f: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==4877==    0x4016ea: initialize_map (list.c:306) ==4877==    0x400b99: main (initialization.c:14) ==4877==  ==4877== invalid write of size 4 ==4877==    @ 0x40172d: initialize_map (list.c:313) ==4877==    0x400b99: main (initialization.c:14) ==4877==  address 0x550debc 4 bytes after block of size 2,584 alloc'd ==4877==    @ 0x4c2db8f: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==4877==    0x4016ea: initialize_map (list.c:306) ==4877==    0x400b99: main (initialization.c:14) ==4877==   valgrind: m_mallocfree.c:277 (mk_plain_bszb): assertion 'bszb != 0' failed. valgrind: caused program erroneously writing past end of heap block , corrupting heap metadata.  if fix invalid writes reported memcheck, assertion failure go away.  please try before reporting bug. 

the initialize code following:

worldmap* initialize_map(game_settings* settings) {        int maxplayers = 10;     int rows = settings->rows;     int cols = settings->cols;     worldmap* world = malloc(sizeof(worldmap)); //free in cleanup_map     if(world != null)     {            world->rows = rows;         world->cols = cols;         world->map = malloc(sizeof(cell)*rows*cols); //free in cleanup_map         for(int = 0; < rows;i++)         {             for(int j = 0; j < cols;j++)             {             cell* cell = index_map(world, j, i); //cell* world->map + offset             cell->type = cell_dirt;             cell->owner =  0;             }         }         world->players = 0;         world->score = null; //null pointer, since pointer set in later function         world->playerturns = null; //null pointer, since pointer set in later function         return world;     }     else{printf("no world found.\n");} } 

the additions initialize_map() code are: int maxplayers, world->players, world->score , world->playerturns, rest of code valid when running in valgrind, without these addition. in code index_map() returns cell* w->map+y_offset + x_offset

i read in several other posts might have allocating integer values , integer pointers again seperatly (i tried on part gave invalid write error):

world->map = malloc(sizeof(cell)*rows*cols); for(int = 0; < rows;i++) {     for(int j = 0; j < cols;j++)     {        cell* cell = index_map(world, j, i); //cell8 world->map + offset        cell->type = malloc(sizeof(celltype);        *(cell->type) = cell_dirt;        cell->owner =  malloc(sizeof(int);        *(cell->type) = 0;     } } 

when trying compile code, folllowing error appears:

"invalid type argument of unary ‘*’ (have ‘unsigned int’) *(cell->type) = 0; "

these structs used:

cell struct:

typedef struct{     celltype type;     unsigned int owner; } cell; 

worldmap struct:

typedef struct {     cell* map;     unsigned int rows;      unsigned int cols;     int players;     int* score;     int* playerturns; } worldmap; 

the cell struct remains unchanged , worldmap struct consisted of cell* map, unsigned int rows , unsigned int cols.

edit index_map code:

cell* index_map(worldmap* w, int x, int y) {        cell* place;     place = w->map + x*w->cols + y;     return place; } 

the valgrind error saying you're accessing memory beyond bounds of map you've allocated. , you've included index_map function it's quite clear why too.

imagine if map 5 columns 2 rows - 10 cells in size. means x , y 4 , 1. function calculates 4*5+1 return 21st cell... code work out should this

place = w->map + x*w->rows + y; 

or

place = w->map + x + y*w->cols; 

which give 4*2+1 or 4+1*5 = 9th cell.

as the other problems mention, don't need allocate memory values because they're not pointers. example

cell->owner =  malloc(sizeof(int); 

cell->owner unsigned int can cell->owner = 10; assign value.

as don't need de-reference them twice use them either - *(cell->owner) overkill , cell->owner needed.


Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -