Ошибка сегментации с вектором <vector <bool>> & - PullRequest
0 голосов
/ 03 апреля 2020

У меня проблемы, когда я пытаюсь получить выбранные значения в варианте задачи о ранце. Указать c. Я получаю ошибку Segmentation fault (core dumped) в строке if(traza[n][w]

Это мой код:

void getSelection(vector<double> &ws, vector<vector<bool>> &traza, vector<bool> &sol){
    unsigned nobjects = traza.size()-1;
    int w = traza[0].size()-1;

    for(int n=nobjects;n>0; n--){
        if(traza[n][w]){
            cerr << "basura" << endl;
            sol[n-1]=true;
            w-=ws[n-1];
        }
        else{
            sol[n-1]=false;
        }
    }
}

Это вывод отладчика:

(gdb) r -f 2.problem 
Starting program: /home/antonio/Escritorio/ADA/Prac6/maxsum -f 2.problem

Breakpoint 1, getSelection (ws=std::vector of length 10, capacity 16 = {...}, 
    traza=std::vector of length 11, capacity 11 = {...}, 
    sol=std::vector<bool> of length 0, capacity 0) at maxsum.cc:183
183             if(traza[n][w]){
(gdb) s
std::vector<bool, std::allocator<bool> >::operator[] (this=<optimized out>, 
    __n=<optimized out>) at maxsum.cc:183
183             if(traza[n][w]){
(gdb) s
std::_Bit_iterator::operator* (this=<optimized out>, this=<optimized out>)
    at /usr/include/c++/7/bits/stl_bvector.h:231
231         { return reference(_M_p, 1UL << _M_offset); }
(gdb) s
std::vector<bool, std::allocator<bool> >::operator[] (this=<optimized out>, 
    __n=<optimized out>) at /usr/include/c++/7/bits/stl_bvector.h:859
859                            + __n / int(_S_word_bit), __n % int(_S_word_bit));
(gdb) s
std::_Bit_iterator::operator* (this=<optimized out>, this=<optimized out>)
    at /usr/include/c++/7/bits/stl_bvector.h:231
231         { return reference(_M_p, 1UL << _M_offset); }
(gdb) s
getSelection (ws=std::vector of length 10, capacity 16 = {...}, 
    traza=std::vector of length 11, capacity 11 = {...}, 
    sol=std::vector<bool> of length 0, capacity 0) at maxsum.cc:183
183             if(traza[n][w]){
(gdb) s
189                 sol[n-1]=false;
(gdb) s
std::vector<bool, std::allocator<bool> >::operator[] (this=<optimized out>, 
    __n=<optimized out>) at /usr/include/c++/7/bits/stl_bvector.h:859
859                            + __n / int(_S_word_bit), __n % int(_S_word_bit));
(gdb) s
858           return *iterator(this->_M_impl._M_start._M_p
(gdb) s
859                            + __n / int(_S_word_bit), __n % int(_S_word_bit));
(gdb) s
std::_Bit_iterator::operator* (this=<optimized out>, this=<optimized out>)
    at /usr/include/c++/7/bits/stl_bvector.h:231
231         { return reference(_M_p, 1UL << _M_offset); }
(gdb) s
std::vector<bool, std::allocator<bool> >::operator[] (this=<optimized out>, 
    __n=<optimized out>) at /usr/include/c++/7/bits/stl_bvector.h:859
859                            + __n / int(_S_word_bit), __n % int(_S_word_bit));
(gdb) s
getSelection (ws=std::vector of length 10, capacity 16 = {...}, 
    traza=std::vector of length 11, capacity 11 = {...}, 
    sol=std::vector<bool> of length 0, capacity 0) at maxsum.cc:189
189                 sol[n-1]=false;
(gdb) s
std::vector<bool, std::allocator<bool> >::operator[] (this=0x7ffffffedce0, 
    __n=<optimized out>) at maxsum.cc:189
189                 sol[n-1]=false;
(gdb) s
std::_Bit_iterator::operator* (this=<optimized out>, this=<optimized out>)
    at /usr/include/c++/7/bits/stl_bvector.h:231
231         { return reference(_M_p, 1UL << _M_offset); }
(gdb) s
std::vector<bool, std::allocator<bool> >::operator[] (this=0x7ffffffedce0, 
    __n=<optimized out>) at /usr/include/c++/7/bits/stl_bvector.h:859
859                            + __n / int(_S_word_bit), __n % int(_S_word_bit));
(gdb) s
getSelection (ws=std::vector of length 10, capacity 16 = {...}, 
    traza=std::vector of length 11, capacity 11 = {...}, 
    sol=std::vector<bool> of length 0, capacity 0) at maxsum.cc:189
189                 sol[n-1]=false;
(gdb) s
std::_Bit_reference::operator= (__x=false, this=<optimized out>)
    at /usr/include/c++/7/bits/stl_bvector.h:89
89              *_M_p &= ~_M_mask;
(gdb) s

Program received signal SIGSEGV, Segmentation fault.
0x0000000008004e46 in std::_Bit_reference::operator= (__x=false, this=<optimized out>)
    at /usr/include/c++/7/bits/stl_bvector.h:89
89              *_M_p &= ~_M_mask;
(gdb) s

Program terminated with signal SIGSEGV, Segmentation fault.
The program no longer exists.
(gdb) 

У меня есть читайте, что логический вектор в с ++ немного странный. Я не понимаю, почему я не могу ссылаться на конкретный элемент c в этом виде вектора. Большое спасибо:

...