UVA.112 Tree Summing and Runtime Error : For a couple of weeks ago, I got some homeworks from my university and the thing is that, though other problems are accepted on the uva, I cannot figure out what is going wrong with this problem. I've already run the input from udebug website and there was no problem. I double-checked the result and now, I'm sick and tired of solving this issue. 

 Here are details about what has happeded. First of all, I increase the BUFSIZE to 2^20 in order to avoid any memory overflow. The result? Failed. Second, I downsized the size of the element in the stack I made. The result? Failed. Lastly, I removed an eol character of the result just in case. The result? Failed.

    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    #include <string.h>
    
    #define BUFSIZE 16384
    
    typedef struct node {
    	int element[BUFSIZE];
    	int size;
    	int current;
    }Stack;
    
    static Stack *stack;
    static int level;
    static int integer;
    
    bool initialize(void) {
    	if (stack == NULL)
    		stack = (Stack *)malloc(sizeof(Stack));
    
    	stack->size = BUFSIZE;
    	stack->current = 0;
    	return true;
    }
    
    bool push(int number) {
    	if (stack == NULL)
    		return false;
    	if ((stack->current + 1) > stack->size)
    		return false;
    
    	stack->element[stack->current] = number;
    	stack->current++;
    	return true;
    }
    
    int pop() {
    	if (stack->current <= 0)
    		return 0xFFFFFFFF;
    	stack->current--;
    	return stack->element[stack->current];
    }
    
    int sum() {
    	int result = 0;
    	int i;
    	if (stack == NULL)
    		return 0xFFFFFFFF;
    	if (stack->current == 0)
    		return 0xFFFFFFFF;
    
    	for (i = 0; i < stack->current; i++)
    		result += stack->element[i];
    
    	return result;
    }
    
    void replace(char * o_string, char * s_string, char * r_string) {
    	char *buffer = (char *)calloc(BUFSIZE, sizeof(char));
    	char * ch;
    
    	if (!(ch = strstr(o_string, s_string)))
    		return;
    	strncpy(buffer, o_string, ch - o_string);
    	buffer[ch - o_string] = 0;
    	sprintf(buffer + (ch - o_string), "%s%s", r_string, ch + strlen(s_string));
    	o_string[0] = 0;
    	strcpy(o_string, buffer);
    	free(buffer);
    	return replace(o_string, s_string, r_string);
    }
    
    
    int main(void) {
    	char *buffer;
    	char *line;
    	char *restOfTheString;
    	char *token;
    	bool checked = false, found = false;
    
    	int i = 0, j = 0, scannedInteger, result = 0, array[4096];
    
    	buffer = (char *)calloc(BUFSIZE, sizeof(char));
    	restOfTheString = (char *)calloc(BUFSIZE, sizeof(char));
    	line = (char *)calloc(BUFSIZE, sizeof(char));
    	memset(buffer, 0, BUFSIZE);
    	for (i = 0; i < 4096; i++) {
    		array[i] = -1;
    	}
    	level = 0;
    	integer = 0;
    
    	while (fgets(line, sizeof(line), stdin) != NULL) {
    		if (line[0] != '\n') {
    			token = strtok(line, "\n");
    			if (strlen(line) >= 1) {
    				strcat(buffer, token);
    			}
    		}	
    	}
    
    	replace(buffer, " ", "");
    	replace(buffer, "()()", "K");
    
    	strcpy(restOfTheString, buffer);
    	i = 0;
    	while (restOfTheString[i] != 0) {
    		if (level == 0 && !checked) {
    			initialize();
    			sscanf(&restOfTheString[i], "%d%s", &integer, &restOfTheString[0]);
    			i = -1;
    			checked = true;
    		}
    
    		if (restOfTheString[i] == '(') {
    			checked = false;
    			level++;
    		}
    		else if (restOfTheString[i] == ')') {
    			if (restOfTheString[i - 1] != '(')
    				if (pop() == 0xFFFFFFFF)
    					return 0;
    			level--;
    			if (!found && level == 0) {
    				array[j] = 0;
    				j++;
    				free(stack);
    				stack = NULL;
    			}
    			else if (found && level == 0) {
    				array[j] = 1;
    				j++;
    				free(stack);
    				stack = NULL;
    				found = false;
    			}
    		}
    		else if (restOfTheString[i] == '-' && !checked) {
    			if (sscanf(&restOfTheString[i], "%d%s", &scannedInteger, &restOfTheString[0]) == 2) {
    				if (push(scannedInteger) == false)
    					return 0;
    				i = -1;
    			}
    		}
    		else if (restOfTheString[i] >= 48 && restOfTheString[i] <= 57 && !checked) {
    			if (sscanf(&restOfTheString[i], "%d%s", &scannedInteger, &restOfTheString[0]) == 2) {
    				if (push(scannedInteger) == false)
    					return 0;
    				i = -1;
    			}
    		}
    		else if (restOfTheString[i] == 'K') {
    			if ((result = sum()) == 0xFFFFFFFF)
    				return 0;
    			if (result == integer) {
    				found = true;
    			}
    		}
    		i++;
    	}
    	i = 0;
    	while (array[i] != -1) {
    		if (array[i] == 1)
    			printf("yes\n");
    		else if (array[i] == 0)
    			printf("no\n");
    		i++;
    	}
    	return 0;
    }

 Though it is cleary suspicious about the memory usage, I don't know how to track the stack on my system. So if you guys have any idea about this problem, please help me cause I'm stuck and the deadline of this homework is within the next 18 hous. 

Thanks 

 