__static_initialization_and_destruction_0(int, int) () 分段错误

问题描述 投票:0回答:1

这是回溯:

SymbolStack的定义(在Symbols.h中):

#ifndef HW3_SYMBOLS_H
#define HW3_SYMBOLS_H

#include <string>
#include <vector>
#include <iostream>
#include "shared_def.h"

using std::vector; using std::string;
extern int yylineno;

...

class SymbolStack {
public:
    vector<int> pos_stack = vector<int>();
    vector<SymbolTable*> scope_stack = vector<SymbolTable*>();
public:
    SymbolStack();
    ErrorType is_func_valid(const string& func_name, const string& retype, vector<string>* params_types, bool is_override);
    void push(bool is_while, Types retType = T_NONE);
    void pop();
    SymbolTable* get_curr_scope();
    void symbol_document(const string& symbol_name,const string& symbol_type, bool is_func = false,
                         bool is_over = false, vector<string>* func_params_types = nullptr); // add new symbol to the stack
    void func_param_document(const string& symbol_name, const string& symbol_type, int pos); // add new function to the stack
    bool symbol_exists(const string& symbol_name); // does a symbol exists?
    SymbolEntry* get_symbol(const string& symbol_name);
    SymbolEntry* get_func_symbol(const string& func_name, const string& retype, vector<string>* params_types, int* count); // if retype == "" error if count >= 2
   // bool function_exists(const string& name, vector<string>* params_types = nullptr);
    bool check_program();
    ~SymbolStack();
};

...

#endif //HW3_SYMBOLS_H

Types 是在shared_def.h 中定义的枚举。 Symbols.cpp 中的方法:

SymbolStack::SymbolStack(){
    pos_stack.push_back(0); // init to offset = zero
    this->push(false, T_VOID);
    vector<string> print_param = vector<string>();
    vector<string> printi_param = vector<string>();
    print_param.push_back("string");
    printi_param.push_back("int");
    this->symbol_document("print", "void", FUNC, &print_param);
    this->symbol_document("printi", "void", FUNC, &printi_param);
}
void SymbolStack::push(bool is_while, Types retType){
    retType = retType == T_NONE? scope_stack.back()->ret_type : retType;
    SymbolTable* first_scope = new SymbolTable(pos_stack.back(), is_while, retType);
    pos_stack.push_back(pos_stack.back());
    scope_stack.push_back(first_scope);
}

在parser.ypp中调用SymbolStack的构造函数:

%{
    #include <stdio.h>
    #include <iostream>
    #include "types.h"
    #include "hw3_output.hpp"
    extern int yylineno;
    extern int yylex();
    int yyerror(const char* message);
    extern SymbolStack tables;
%}

definitions...
%%
Rule...
%%

tables = SymbolStack();
int main(){
        int res = yyparse();
        bool is_valid_program = tables.check_program();
        if(!is_valid_program){
            output::errorMainMissing();
            exit(0);
        }
        tables.pop();
        return res;
}
int yyerror(const char * message){
    output::errorSyn(yylineno);
    exit(0);
}

我没能使用 gdb 调试它,因为它甚至在调用 main 之前就发生了。如果我删除这一行“this->push(false, T_VOID);”也会发生这种情况来自构造函数。

编辑:我使用的任何源文件中没有任何内容被定义为静态。

c++ segmentation-fault gdb
1个回答
1
投票

我没能使用 gdb 调试它,因为它甚至在调用 main 之前就发生了。

  1. GDB 完美能够调试
    main
    之前发生的崩溃。
  2. 显示 GDB 输出(但您的代码是在没有调试的情况下构建的 (
    -g
    ))。

我使用的任何源文件中没有任何内容被定义为静态。

您有一个 global

tables
变量。崩溃很可能发生在该变量的初始化期间。


您应该能够通过使用

-g
重新构建并在
SymbolStack::SymbolStack
上设置断点来自行调试。

我唯一能想到的是,如果

T_VOID == T_NONE
,那么这一行:

    retType = retType == T_NONE? scope_stack.back()->ret_type : retType;

将使用不存在的

scope_stack.back()
scope_stack
仍然是空的),这应该会崩溃。

附注要了解崩溃,检查崩溃指令 (

(gdb) x/i $pc
) 和寄存器值 (
(gdb) info regs
) 通常很有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.