Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

std::pair returned by std::transform resulting in segfault

I’m trying to transform a vector of strings to a vector of pairs of strings, and I was getting segfault. I’ve tried to narrow it down to a simple test case (below), and I’m sure it’s likely to do with the memory allocation:

#include <string>
#include <vector>
#include <utility>

std::pair<std::string, std::string>
newP( const std::string& foo) {
  return std::make_pair(std::string("Foo"), std::string("Bar"));
}

int main()
{
  std::vector<std::string> vec;
  std::vector<std::pair<std::string, std::string>> pairs;

  vec.push_back("Hello, ");
  vec.push_back("World!");

  std::transform(vec.cbegin(), vec.cend(), pairs.begin(), newP);
}

I get following segfault on FreeBSD 13.1:

Program received signal SIGSEGV, Segmentation fault.
Address not mapped to object.
0x0000000000205fd5 in std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::__is_long (this=0x0) at /usr/include/c++/v1/string:1456
1456            {return bool(__r_.first().__s.__size_ & __short_mask);}
(gdb) bt
#0  0x0000000000205fd5 in std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::__is_long (this=0x0) at /usr/include/c++/v1/string:1456
#1  0x0000000000205f0d in std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::__move_assign (this=0x0, __str="Foo") at /usr/include/c++/v1/string:2463
#2  0x0000000000205ee1 in std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::operator= (this=0x0, __str="Foo") at /usr/include/c++/v1/string:2485
#3  0x0000000000205ded in std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::operator= (
    this=0x0, __p=...) at /usr/include/c++/v1/__utility/pair.h:277
#4  0x0000000000203fe5 in std::__1::transform<std::__1::__wrap_iter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const*>, std::__1::__wrap_iter<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >*>, std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > (*)(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)> (__first=..., __last=...,
    __result=..., __op=0x203c20 <newP(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)>) at /usr/include/c++/v1/__algorithm/transform.h:29
#5  0x0000000000203d96 in main () at pair.cc:19

Could someone please explain what I’m doing wrong ? And if possible, what would be the best way to fix this. I’m fairly out of touch with modern C++.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks!

>Solution :

The vector pairs is empty, and pairs.begin() will return the pairs.end() iterator which can’t be dereferenced.

Set the size of pairs to the same size as vec before calling transform:

pairs.resize(vec.size());
std::transform(vec.cbegin(), vec.cend(), pairs.begin(), newP);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading