B
B
byulent2015-12-24 21:12:23
Programming
byulent, 2015-12-24 21:12:23

Segmentation Fault - Why?

There is a class:

class tree{
private:
    vector<node> nodes;
    vector<wstring> codedString;
    int fmax;
    int inode1;
    int inode2;
public:
    void extractString(wstring str); //Разделяем строку на первичные узлы
    void construct();
    void codeString (wstring str);
    void printCodedString();
    void printFreqTable();
    void printCodeTable();
    void codeNodes();
    void code(node& n, int level);
    void selectNode (wstring str);
    wstring decodeString ();
    void findCode (wstring str);
    node findNode (wstring code);
    bool inNodes (wstring str);
    bool inNodes (int freq);
    void getMinNode1();
    void getMinNode2();
};

The node object itself:
class node{
private:
    int freq, child1, child2;
    wstring sym;
    wstring code;
    bool flag;
public:
    node(int fr, int ch1, int ch2, wstring s, wstring c):freq(fr),child1(ch1),child2(ch2),sym(s),code(c),flag(false){};
    int getFreq();
    int getChild1();
    int getChild2();
    wstring getCode();
    //bool getFlag();
    wstring getSym();
    void incFreq();
    void incCode(int val);
    void toggle();
   //bool operator> (node& n);
   bool operator<= (node& n);
};

A function that creates a vector with nodes:
void tree::extractString (wstring str)
{
    wcout << L"извлекаются узлы из строки" << endl;
    for (int i=0; i<str.size()/2; i++){
        wstring tmp = str.substr (i*2, 2);
        if (inNodes(tmp)) selectNode(tmp);
        else {
            node n  (1, -1, -1, tmp, L"");
            nodes.push_back(n); //тут и возникает ошибка
        }
    }
    wcout << L"узлы извлечены" << endl;
}

main, where all this is called:
int main(){
  setlocale (LC_ALL, "");
  tree t;
  wstring str;
  wcout << L"Введите строку" <<endl;
  getline(wcin, str);
  t.extractString(str);
        //и так далее
}

So, if the field codeof the object nodehas the type wstring, as it is now, a segmentation error pops up in the function on the second node.
Введите строку
Корабли лавировали, лавировали, да не вылавировали
извлекаются узлы из строки
Ошибка сегментирования (сделан дамп памяти)

It is worth replacing it with int(and at the same time changing the implementation of everything related to this field) - everything is successfully created, but this type is not suitable for my purposes.
Question: why is a vector of objects with one string field created, but not with two? What's wrong here?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
abcyu, 2015-12-24
@byulent

int is always passed by value unless told to be passed by reference.
What about vectors and strings? How do you think?

J
jcmvbkbc, 2015-12-24
@jcmvbkbc

why is a vector of objects with one string field created, but not with two? What's wrong here?

Without the full source text, all that can be said is that the bug is most likely somewhere else.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question