In the following code I have a problem. When I give it a vector that is still completely empty the code crashes, because vector.size() - 1 can't be negative, hence it wraps around. Because the vector is empty accessing container[0] is invalid.
using namespace std;
template<typename T, typename A>
std::string vec_to_python_list(
const std::string& name,
const vector<T, A>& container
) {
std::stringstream stream(ios::out);
stream << name << " = [" << '\n';
for (typename vector<T, A>::size_type i = 0; i < container.size() - 1; i++)
stream << "\t" << container[i] << ",\n";
if (name.size() > 0)
stream << "\t" << container[container.size() - 1] << "\n";
stream << "]";
return stream.str();
}
My question is about the output that it produces.
If I compile with g++ on Ubuntu-16.04
g++ -Wall -Wextra -std=c++14 -g -fsanitize=address -O0 -o test_kmeans test_kmeans.cpp
I obtain the next useful info:
#0 0x402a56 in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > vec_to_python_list<double, std::allocator<double> >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::vector<double, std::allocator<double> > const&) /home/hetepeperfan/programming/cpp/kmeans/test_kmeans.cpp:46
#1 0x401f07 in main /home/hetepeperfan/programming/cpp/kmeans/test_kmeans.cpp:66
#2 0x7f393881f82f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
#3 0x401ba8 in _start (/home/hetepeperfan/programming/cpp/kmeans/test_kmeans+0x401ba8)
However if i compile with clang++ (still on Ubuntu-16.04)
clang++ -Wall -Wextra -std=c++14 -g -fsanitize=address -O0 -o test_kmeans test_kmeans.cpp
I get less useful results:
#0 0x4eecae (/home/hetepeperfan/programming/cpp/kmeans/test_kmeans+0x4eecae)
#1 0x4ee056 (/home/hetepeperfan/programming/cpp/kmeans/test_kmeans+0x4ee056)
#2 0x7f6ed883a82f (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
#3 0x419838 (/home/hetepeperfan/programming/cpp/kmeans/test_kmeans+0x419838)
What am I do'ing wrong so that g++ with -fsanitize=address works and clang++ doesn't produce as useful results, It seems like the debugging symbols aren't added?
EDIT The debug symbols seem to be present, because with gdb I can easily step through the code and with the --tui gdb option I can see my code, so that isn't the issue.