So the function I am working with is part of a bigger project (checkers game).What I am trying to do is save memory for 4 variables type char, and save more memory as the game progresses.
I am using malloc for the initial allocation of memory and then realloc to save more.
When I execute it I get this error:
Error in `./damas': realloc(): invalid pointer: 0x00007ffec3209fe7 ***
Aborted (core dumped)
void list(char *pcolumn_init,char *pcolumn_move, char *pline_init,char *pline_move,int *pturn) { int res=5; char *lista,*temp; if(*pturn==1){ temp=(char *)malloc(res*(sizeof(char))); if(!temp) printf("Could not save memory\n"); temp[0]=*pline_init; temp[1]=*pcolumn_init; temp[2]=*pline_move; temp[3]=*pcolumn_move; }else{ res+=4; lista=realloc(temp,res*sizeof(char)); temp=lista; if(lista==NULL) free(temp); lista[res-4]=*pline_init; lista[res-3]=*pcolumn_init; lista[res-2]=*pline_move; lista[res-1]=*pcolumn_move; } }
If someone could help me fix this and understand what I am doing wrong
Edited by dargueta, 21 December 2015 - 10:52 PM.