Goal is to create a balanced symbol checker, program looks for input of these symbols <{[(, then check to see if they are closed. each time one of these is encountered, it is to be pushed onto the stack. Once the stack is full, more memory needs to be allocated. I'm not sure where this error is coming from, but I believe it might be from my push function. I am super confused and have searched everywhere(looked at every question with a similar title on here and tried suggested solutions) to try to fix it. From what I could gather, this error means I am trying to free something twice, but I cannot figure out where that would be happening. I have tried multiple different ways of doing this and nothing seems to work. please help.
Also slightly confused on how to resize a dynamic array. Going into this I thought you made a new temp* pointing to the array contents, resized the original with malloc which erases all contents, then put the contents saved in temp back in. I have seen it done several different ways and am unsure which way to use in which context. Thank you.
typedef struct{ //for reference
char *darr;
int size;
int top;
}
stack;
void push (stack *s, char tsymbol){
if (s->top == s->size){ //if stack is full
char *temp = (char*)malloc(sizeof(char)*s->size);
temp = s->darr;
free(s->darr);
s->darr = temp;
s->size += 2;
}
s->darr[++(s->top)] = tsymbol;
//s->top = s->top + 1;
}
different approach
if (s->top == s->size-1){ //if stack is full
char *pTemp;
pTemp = (char*)malloc(sizeof(char)*((s->size)+2));
int i;
for (i=0; i<(s->top); i++){
pTemp[i] = s->darr[i];
}
free (s->darr);
s->darr = pTemp;
}
s->darr[s->top] = tsymbol;
s->top = s->top + 1;
}