Not compiling code, really long GDB error message.

Hello,

I'm trying to debug this code but the GDB error is so long I'm having a hard time to decipher it, Vim doesn't seem to want to compile it with GCC and compiler checker specified to compile for c++ 11 standard although c++ shell seems to compile it just fine.

Error : Program received signal SIGSEV, Segmentation Fault.
0x00007ffff7b769bb in std::__cxxll::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::__cxxll:basic_string<char, std::char_traits<char>, std::allocator<char> > const& () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6

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
#include <iostream>
#include <string>
#include <vector>


std::string mirror (std::string word)
{
  if (word.length() <= 0 )
  {
    return {};
  }
  else
  {
    char lastLetter = word.back(); // Get last index of the string.
    word.pop_back(); // Remove last index of the string
    return lastLetter + mirror (word); // Put last letter on the front of the word and pass the new word without the last letter to the next function.
  }
}

std::vector<std::string> splitString( std::vector<std::string> split, std::string to_split )
{
  std::size_t position = 0, space;
  while((space = to_split.find_first_of(' ',position)) != std::string::npos)
  {
    split.push_back(to_split.substr(position, space - position));
    position = space + 1;
  }
  split.push_back(to_split.substr(position));
  return split;
}

void outputString ( std::string to_split )
{
  std::vector<std::string> splitWords = splitString( splitWords, to_split );
  for ( int i=0; i<splitWords.size(); i++)
  {
    std::string output = mirror(splitWords[i]);
    std::cout << output << " "; 
  }
}

int main()
{
  std::string test = "one two three four";
  outputString ( test );
  return 0;
}
Last edited on
Posting the error may help us help you.
Turn the errors and warnings up in C++ shell. Your function outputString doesn't return the string it promised to return.

For errors - see line 35. Should be
i<splitWords.size()
not
i<splitWords.size()-1
(Alternatively, use <= rather than <)
Yes, I have fixed that but the error remains, I had a similar issue recently and the cause was accessing a vector without any elements but I'm pushing back and returning the vector so that shouldn't be an issue.
Last edited on
Could you post the error, the error I see in a online gdb compiler is this

/home/a.out: relocation error: /home/a.out: symbol _ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE9_M_createERmm, version

GLIBCXX_3.4.21 not defined in file libstdc++.so.6 with link time reference

This points to an environment issue rather than a code issue see the link in this article
https://github.com/typora/typora-issues/issues/504
I've updated the code it compiles fine in C++ Shell still and in Vim nothing again, I've tried to reinstall libstdc++ but no luck.

Line 34:
std::vector<std::string> splitWords = splitString( splitWords, to_split );
How does this work? You appear to have splitWords on the RHS. You don't need it as an argument to the function: you need to declare a vector to return the result in the function splitString(...) itself.
Last edited on
There's two last frames on GDB.

#6 0x00000000004014a9 in OutputString (to_split="one two three four") at mirror.cpp:34
#7 0x000000000040168b in main () at mirror.cpp:45

If I break at line 34 and do next I jump straight to segmentation fault that leads me to I am doing something wrong with the vector.

 
std::vector<std::string> splitWords = splitString( splitWords, to_split );


1
2
3
Line 34:
std::vector<std::string> splitWords = splitString( splitWords, to_split );
How does this work? You appear to have splitWords on the RHS. You don't need it as an argument to the function: you need to declare it in the function splitString(...) itself. 


This has been the bug, thank you for your help and for my inability to update the code for You. I was contemplating on what is going wrong and forgot to update the source-code, so it didn't reflect the fact that I have changed the return type of the function to void instead of std::string on the outputString.

I have removed the need to pass the vector as an argument in the splitString function and just declared a local vector within the function.
Last edited on
Try this one:
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
#include <iostream>
#include <string>
#include <vector>


std::string mirror (std::string word)
{
  if (word.length() <= 0 )
  {
    return {};
  }
  else
  {
    char lastLetter = word.back(); // Get last index of the string.
    word.pop_back(); // Remove last index of the string
    return lastLetter + mirror (word); // Put last letter on the front of the word and pass the new word without the last letter to the next function.
  }
}

std::vector<std::string> splitString( std::string to_split )           //<=======
{
  std::vector<std::string> split;                                      //<=======

  std::size_t position = 0, space;
  while((space = to_split.find_first_of(' ',position)) != std::string::npos)
  {
    split.push_back(to_split.substr(position, space - position));
    position = space + 1;
  }
  split.push_back(to_split.substr(position));
  return split;
}

void outputString ( std::string to_split )
{
  std::vector<std::string> splitWords = splitString( to_split );       //<=======
  for ( int i=0; i<splitWords.size(); i++)
  {
    std::string output = mirror(splitWords[i]);
    std::cout << output << " "; 
  }
}

int main()
{
  std::string test = "one two three four";
  outputString ( test );
}
Note:

Clang 5 emits the following warning:
01-191944.cxx:29:53: warning: variable 'splitWords' is uninitialized when used
      within its own initialization [-Wuninitialized]
  std::vector<std::string> splitWords = splitString(splitWords, to_split);

But G++ does not warn here.

You may find it useful to install at least one other compiler: often, there is an appreciable difference between the quality of any particular diagnostic. Also, consider making sure to turn on all useful warnings, using flags like -Wall -Wextra -pedantic-errors.
Last edited on
Topic archived. No new replies allowed.