supertuxkart / stk-code

The code base of supertuxkart
Other
4.36k stars 1.03k forks source link

Use of std::vector<bool> #5053

Closed Alayan-stk-2 closed 1 week ago

Alayan-stk-2 commented 1 month ago

I noticed that in race_manager.hpp, m_reverse_track is defined as a std::vector of bools

Unfortunately, in C++, such a vector is using an optimized data structure that uses only 1 bit to store each bool. While that sounds good on paper, it also means that some of the common assumptions about std::vector don't hold true in this specific case.

This leads to a series of compiler warnings for each place in the code that calls the "SetReverse" function, which does this:

        m_reverse_track.clear();
        m_reverse_track.push_back(r_t);

I am not sure if this is leading to any functional bug, but some change, either to the use of push_back (which is what is flagged by the compiler) or to the data structure itself, to get rid of the warnings would be welcome.

Alayan-stk-2 commented 1 week ago

For reference the warning

Dans la fonction membre statique « static _Up* std::__copy_move<_IsMove, true, std::random_access_iterator_tag>::__copy_m(_Tp*, _Tp*, _Up*) [avec _Tp = long unsigned int; _Up = long unsigned int; bool _IsMove = false] »,
    mis en ligne depuis « _OI std::__copy_move_a2(_II, _II, _OI) [avec bool _IsMove = false; _II = long unsigned int*; _OI = long unsigned int*] » à /usr/include/c++/13.2.1/bits/stl_algobase.h:506:30,
    mis en ligne depuis « _OI std::__copy_move_a1(_II, _II, _OI) [avec bool _IsMove = false; _II = long unsigned int*; _OI = long unsigned int*] » à /usr/include/c++/13.2.1/bits/stl_algobase.h:533:42,
    mis en ligne depuis « _OI std::__copy_move_a(_II, _II, _OI) [avec bool _IsMove = false; _II = long unsigned int*; _OI = long unsigned int*] » à /usr/include/c++/13.2.1/bits/stl_algobase.h:540:31,
    mis en ligne depuis « _OI std::copy(_II, _II, _OI) [avec _II = long unsigned int*; _OI = long unsigned int*] » à /usr/include/c++/13.2.1/bits/stl_algobase.h:633:7,
    mis en ligne depuis « std::vector<bool, _Alloc>::iterator std::vector<bool, _Alloc>::_M_copy_aligned(const_iterator, const_iterator, iterator) [avec _Alloc = std::allocator<bool>] » à /usr/include/c++/13.2.1/bits/stl_bvector.h:1306:28,
    mis en ligne depuis « void std::vector<bool, _Alloc>::_M_insert_aux(iterator, bool) [avec _Alloc = std::allocator<bool>] » à /usr/include/c++/13.2.1/bits/vector.tcc:946:34,
    mis en ligne depuis « void std::vector<bool, _Alloc>::push_back(bool) [avec _Alloc = std::allocator<bool>] » à /usr/include/c++/13.2.1/bits/stl_bvector.h:1124:17,
    mis en ligne depuis « void RaceManager::setReverseTrack(bool) » à /STK/stk-code/src/race/race_manager.hpp:484:34,
    mis en ligne depuis « virtual void HelpScreen1::eventCallback(GUIEngine::Widget*, const std::string&, int) » à /STK/stk-code/src/states_screens/help/help_screen_1.cpp:69:44:
/usr/include/c++/13.2.1/bits/stl_algobase.h:437:30: attention: « void* __builtin_memmove(void*, const void*, long unsigned int) » qui forme le décalage 8 est hors des limites [0, 8] [-Warray-bounds=]
  437 |             __builtin_memmove(__result, __first, sizeof(_Tp) * _Num);