简体   繁体   中英

I am getting segmentation fault. I have made the code to see if the brackets are balanced or not. C++

I have written this code to check for balanced brackets in an expression. I have used linked list implementation of stacks. I am getting a segmentation error. Please explain why do we get segmentation error in the first place, I am newbie and having a hard time understanding the concept of segmentation errors and memory management in general.

#include <iostream>
using namespace std;
struct stack {
    char data;
    struct stack *next;
};
struct stack *top = NULL;
void push(char data) {
    struct stack *temp = (struct stack *)malloc(sizeof(struct stack));
    temp->data = data;
    temp->next = top;
    top = temp;
    return;
}
void pop() {
    struct stack *temp = top;
    top = top->next;
    return;
}
int isEmpty() {
    if (top == NULL)
        return 1;
    else
        return 0;
}
int main() {
    int i = 0;
    char array[250];
    cout << "Enter code and end with $ sign : " << endl;
    cin.getline(array, 250, '$');
    while (array[i] != '\n') {
        if (array[i] == '{' || array[i] == '[' || array[i] == '(') {
            push(array[i]);
        } else if (array[i] == '}' || array[i] == ']' || array[i] == ')') {
            if (array[i] == '}' && top->data == '{')
                pop();
            else if (array[i] == ']' && top->data == '[')
                pop();
            else if (array[i] == ')' && top->data == '(')
                pop();
            else {
                cout << "Not Balanced" << endl;
                return 0;
            }
        }
        i++;
    }
    if (isEmpty() == 1)
        cout << "Balanced" << endl;
    else
        cout << "Not Balanced" << endl;
    return 0;
}

So I will begin by explaining what a segmentation fault means and why it happens. A segmentation fault happens when a program tries to read memory that it's not allowed to read. This can happen if you try to get an array element that is outside of the allocated array and it can also happen if you dereference a null pointer.

I think the problem in your case might be that your loop fails to end in the right place and ends up accessing array elements outside of the 250 spaces allocated. according to this reference http://www.cplusplus.com/reference/istream/istream/getline/ , getline() doesn't actually include the newline character into the array, however it always appends a null character '\\0'.

Try changing while(array[i]!='\\n') to while(array[i]!='\\0') and let me know if that fixes it. If not I will keep looking for more errors. Your code is not super readable and there are a lot of places that something could go wrong. I would recommend adding empty lines between each of your functions.

Also, as molbdnilo mentioned, you will also get a segmentation fault if you have too many closing braces, since in this case top will be equal to null and your program will try to dereference top using top->data in your condition

Also I notice you're using a lot of C conventions such as struct, malloc(), and NULL. It works fine, but make sure to recognize that there is a difference between C and C++ and try to stick to C++ sources when learning so you don't get confused. Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM