First, well done indeed for using GDB to track down problems. I really applaud that. It's good to see someone actually doing their own debugging. Well done.
Let's go a little further. In gdb, at this point, enter the command: bt
This stands for backtrace. It will show you the call stack. It will look something like this:
1 2 3 4
|
(gdb) bt
#0 0x00007ffff7b49d10 in std::basic_istream<char, std::char_traits<char> >& std::operator>><char, std::char_traits<char> >(std::basic_istream<char, std::char_traits<char> >&, char*) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#1 0x0000000000400e6e in vectorInput (v=std::vector of length 0, capacity 0, n=2) at 233.cpp:27
#2 0x0000000000400fbb in main () at 233.cpp:44
|
You can see the call stack of functions at the moment everything broke. At frame #0, what you already saw. Something gone wrong deep in the std library. Hmm. Unlikely to be a bug in the library. So look up one, to frame #1. My command in bold again.
1 2 3
|
(gdb) frame 1
#1 0x0000000000400e6e in vectorInput (v=std::vector of length 0, capacity 0, n=2) at 233.cpp:27
27 std::cin >> v[i].name;
|
Aha! This is in YOUR code, so this is where you call the std library function. Maybe you're doing something wrong here.
Let's look at v:
1 2
|
(gdb) print v
$1 = std::vector of length 0, capacity 0
|
v is a vector of size zero. Aha! We're trying to set a value in a vector of size zero!
Yup, we're trying to access v[0] which does not exist. That's why the segfault. We're trying to access the first element of the vector, but the vector is of size zero; it has no elements.
So you need to make sure that v has a non-zero size at this point.
I see that you create v in global space with size zero. Then, you make
vectorInitialization function operate on a
copy of v (because that's how pass-by-value functions work; the function gets a
copy), which you then return but ignore. The original global v is unchanged.