Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

segmentation fault when moving std::vector [closed]

The following program crashes with segmention fault:

#include <iostream>
#include <vector>

using namespace std;

struct data
{
  data() : a(random()), b(random()), v({random(), random(), random()}) {}
  data(data&& m) noexcept : a(m.a), b(m.b), v(std::move(m.v)) { }

  long int a;
  long int b;
  std::vector<long int> v;
};

data&& randomize()
{
  srandom(time(0));
  data d;
  d.a = random();
  return std::move(d);
}

int main( int argc, char** argv )
{
  data d = randomize();
  cout << d.a << " " << d.b << endl;
  return 0;
}

The code is compiled with g++ version 4.7.2 (Debian 4.7.2-5):

g++ -std=c++11 -g test.cpp

What am I doing wrong? The problem seems to be in std::vector move constructor, cause everything works fine without it. It looks like data object from randomize() is destroyed when function finishes, but shouldn't it rather be moved to the data object in main first?

like image 265
Pavel Davydov Avatar asked Mar 29 '13 12:03

Pavel Davydov


People also ask

How do you fix segmentation fault in C++?

See if your compiler or library can be set to check bounds on [i] , at least in debug mode. Segmentation faults can be caused by buffer overruns that write garbage over perfectly good pointers. Doing those things will considerably reduce the likelihood of segmentation faults and other memory problems.

Why am I getting segmentation faults C++?

A segmentation fault occurs when your program attempts to access an area of memory that it is not allowed to access. In other words, when your program tries to access memory that is beyond the limits that the operating system allocated for your program.

What are three kinds of pointers that can cause a segmentation fault?

The following are some typical causes of a segmentation fault: Attempting to access a nonexistent memory address (outside process's address space) Attempting to access memory the program does not have rights to (such as kernel structures in process context) Attempting to write read-only memory (such as code segment)

Does C++ have segmentation fault?

Core Dump (Segmentation fault) in C/C++ Core Dump/Segmentation fault is a specific kind of error caused by accessing memory that “does not belong to you.” When a piece of code tries to do read and write operation in a read only location in memory or freed block of memory, it is known as core dump.


1 Answers

This function:

data&& randomize()
{
    // ...
    data d
    // ...
    return std::move(d);
}

Returns a reference to a local object which is going to be destroyed when the call returns. Therefore, your program has Undefined Behavior. Therefore, the returned reference will be dangling by the time the move constructor of data is invoked here:

data d = randomize();

You should return a value of type data, and you shouldn't explicitly invoke std::move():

data randomize()
{
    // ...
    data d
    // ...
    return d;
}

This way, you will also give the compiler the opportunity to performed (Named) Return Value Optimization, possibly resulting in no call to the move constructor at all.

like image 138
Andy Prowl Avatar answered Sep 28 '22 16:09

Andy Prowl