Understand GDB error caused by my code.

Hello,

I'm writing a program that takes an n amount of cubes and builds a tower out of them based on their length and colour but it seems I'm doing something wrong when entering the data into data members of struct.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <vector>
#include <string>
  struct Cube // Create a struct to store cube's data members.
  {
    char name[20];
    char colour[10]; 
    int length;
  };

  std::vector<Cube> v;

  std::vector<Cube> vectorInitialization( std::vector<Cube> v, int n )
  {
    for ( int i=0; i<n-1; i++)
    {
    v.push_back(Cube());
    }
    return v;
  }

  std::vector<Cube> vectorInput ( std::vector<Cube> v, int n )
  {
    for ( int i=0; i<n-1; i++)
    {
      std::cout << "Enter name of the cube: " << std::endl;
      std::cin >> v[i].name;
      std::cout << "Enter colour of the cube: " << std::endl;
      std::cin >> v[i].colour;
      std::cout << "Enter lenght of the cube: " << std::endl;
      std::cin >> v[i].length;
    }
    return v;
  }
      
  
int main()
{
  int n = 0;
  std::cout << "Enter number of cubes: " << std::endl;
  std::cin >> n;
  
  vectorInitialization(v, n);
  vectorInput(v,n);
  for (int i=0; i<n-1; i++)
  {
    std::cout << v[i].name << std::endl;
  }
  return 0;
}


(gdb) Program received signal SIGSEGV, Segmentation fault.
0x00007ffffafacb0 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
Last edited on
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!

1
2
(gdb) print i
$2 = 0


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.
Last edited on
Thank you for making it clear to me, how would I change the size of the vector without having to implicitly set its size but by changing it based on the size of N being the amount of cubes ?

Should I just remove the global variable and work solely with the local vector and pass it to another function ?
Last edited on
Definitely remove global variables. They are, at risk of simplifying, bad.

how would I change the size of the vector
v.resize(n);

http://www.cplusplus.com/reference/vector/vector/resize/

Thank you, I don't think I deserve a praise because as a Computer Science student for over a year I should know more than that, choose C++ over Python which makes it much more painful to learn but more rewarding.

I'm reading Bjarne Stroustrup's Programming Principles and Practice Using C++, do you recommend any other literature to read in order to grasp a larger understanding of the language?

PS. Sorry for Off-Topic.
Topic archived. No new replies allowed.