2022.11.18 macos c++ gcc, debug to find which line caused the "segment fault"
gist: https://gist.github.com/JaosnHsieh/69ae210db4f43f133a08724882d6adc9
main.cpp
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector<vector<int>> v;
v[2][2];
return 0;
}
Steps
1. gcc -lstdc++ -std="c++11" ./main.cpp -o main -g
##compile with -g for debug
2. lldb --file ./main.
## use lldb instead of gdb ( tried gdb and found no way to get it right )
3. (in lldb repl)
1. r
2. bt
after "bt", we can see which lines caused the error
messages from lldb , we can see main.cpp:7:3 listed
lldb --file ./main
(lldb) target create "./main"
Current executable set to '/Users/jh/git/cpp-test/main' (x86_64).
(lldb)
Current executable set to '/Users/jh/git/cpp-test/main' (x86_64).
(lldb) r
Process 67186 launched: '/Users/jh/git/cpp-test/main' (x86_64)
Process 67186 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x30)
frame #0: 0x0000000100003430 main`std::__1::vector<int, std::__1::allocator<int> >::operator[](this=0x0000000000000030 size=0, __n=2) at vector:1572:18
1569 vector<_Tp, _Allocator>::operator[](size_type __n) _NOEXCEPT
1570 {
1571 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
-> 1572 return this->__begin_[__n];
1573 }
1574
1575 template <class _Tp, class _Allocator>
Target 1: (main) stopped.
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x30)
* frame #0: 0x0000000100003430 main`std::__1::vector<int, std::__1::allocator<int> >::operator[](this=0x0000000000000030 size=0, __n=2) at vector:1572:18
frame #1: 0x00000001000033c3 main`main at main.cpp:7:3
frame #2: 0x00007ff812929310 dyld`start + 2432
from:
https://stackoverflow.com/a/63943980/6414615
https://stackoverflow.com/a/2876374/6414615
tools version
gcc -v
Apple clang version 14.0.0 (clang-1400.0.29.102)
Target: x86_64-apple-darwin22.1.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
lldb --version
lldb-1400.0.30.3
Apple Swift version 5.7 (swiftlang-5.7.0.127.4 clang-1400.0.29.50)
macos 13.0.1
gist: https://gist.github.com/JaosnHsieh/69ae210db4f43f133a08724882d6adc9