What wrong with my code,got run time error with case "1-(2+3-(4+(5-(1-(2+4-(5+6))))))",but correct in dev c++.


  • 0
    X
    class Solution {
    public:
    	stack<char> op;
    	char *cur;
    	bool cmp(char front, char rear){
    		if (front == '+' || front == '-'){
    			if (rear == '(') return false;
    			else return true;
    		}
    		else if (front == '(' || front == '#')
    		{
    			if (front=='#' && rear=='#'){
    				op.pop(); cur++;
    			}
    			if (front == '(' && rear == ')'){
    				op.pop(); cur++;
    			}
    			return false;
    
    		}
    	}
    	
    
    	int calculate(string s) {
    		stack<int> num;
    	
    		int itmp;
    		string stmp;
    		char calc;
    		int num1, num2, res, len;
    		stringstream ss;
    		char *head = const_cast<char*>(s.c_str());
    		char *start;
    		cur = start = head;
    		 strcat(head, "#");
    		op.push('#');
    		while (*cur != '\0' ){
    			while (*cur == ' ')
    				cur++;
    			head = cur;
    			while (isdigit(*cur)){	
    				cur++;
    			}
    		
    		if (head != cur){
    				stmp = string(s, head - start, cur - head);
    				ss.clear();
    				ss << stmp;
    				ss >> itmp;
    				num.push(itmp);
    				head = cur;
    			}
    
    			if (!op.empty()  )
    				if (cmp(op.top(), *cur)){
    					num1 = num.top();
    					num.pop();
    					num2 = num.top();
    					num.pop();
    					calc = op.top();
    					op.pop();
    
    					while (*cur == ')' &&op.top()=='(' ) { op.pop(); cur++; head++; }
    					if (*cur == '#'&&op.top() == '#')  {
    						op.pop(); cur++;
    					}
    					if (calc == '+')
    						num2 += num1;
    					else
    						num2 -= num1;
    					num.push(num2);
    				}
    
    				if (!op.empty())
    				{
    					
    					
    					if (*cur != '#'&&*cur!=')'){
    						op.push(*cur);
    						cur++; head++;
    					}
    				}
    		}
    		int val = num.top();
    		return val;
    	}
    };

  • 0
    B

    I would try to compile in debug mode and release mode and see if visual C++ flag some stuff. Maybe there are some unitialized memory, and since you're working with low level string manipulation.

    If you cannot figure out where the problem is, I would convert strcat and friends to std::string operations which should be less risky. To walk over the string you could index into the std::string as well with an integer, so that you don't risk going over the end of the array while incrementing a pointer (the one you get form calling c_str())

    Good luck !

    If you happen to have a linux box around, I'd run your program inside of valgrind, and google (microsoft valgrind equivalent) to search for such a tool on Windows.


  • 0
    B

    I assume that dev c++ is Visual studio ... if it's not well my comment is not as relevant :)


  • 1

    I've tested your program on a mac (g++) and it runs okay. However it gives the following error when run on ubuntu:

    *** Error: double free or corruption (!prev): 0x0000000001504360 ***
    

    I suspect it is due to this line:

    strcat(head, "#");
    

    s.c_str() returns a pointer that points to read-only data, and you override this restriction by forcing a cast from const char * to char *. When you write to the data, then it becomes undefined behavior because you have no idea how the string object works internally. For example, it may keep track of the length of a string and may use this information to deallocate memory, which is no longer true since you append to the string without updating its length.

    If you must write to the string, you should make a copy of it first using strdup:

    char *head = strdup(s.c_str())
    

    Then you are safe to append or do whatever you want with the string. Don't forget to call free when you are done to avoid leaking memory.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.