Why vector<int> variable returned by const ref doesn't work


moth

Here is my function:

const vector<int>& getVInt(){
  vector<int> vint;
  (...)
  return vint;
}

and,

  vector<int> x = getVInt();

return:

Throwing 'std:: out_of_range' what(): terminate call after
vector::_M_range_check instance

and

const vector<int>& x = getVInt();

returns nothing (a vector with a size different from 0 but no value, when I use x.at(i)).

I've looked on forums, but the answers about temporary and constant references don't help me understand this.

Thank you.

shoe

You are returning a reference to a local object. That is undefined behavior. Instead, the copy can be eliminated by RVO (return value optimization) .

std::vector<int> getVInt(){
    std::vector<int> vint;
    // …
    return vint;
}

Related


why const doesn't work

Rafflesia class Student{ public: Student(); Student(string name, int score); string getName(); int getScore(); void insert(string name, int score); friend ostream& operator<<(ostream& os, const Student& student){ os << "Name: "<

why const doesn't work

Rafflesia class Student{ public: Student(); Student(string name, int score); string getName(); int getScore(); void insert(string name, int score); friend ostream& operator<<(ostream& os, const Student& student){ os << "Name: "<

Why ref doesn't work in FlatList component?

Movie import React, { Component } from 'react'; import { AppRegistry, Alert, FlatList, StyleSheet, Text, View } from 'react-native'; export default class FlatListBasics extends Component { _onscroll() { this.flatList.scrollToEnd( { animated: false } )

Why ref doesn't work in FlatList component?

Movie import React, { Component } from 'react'; import { AppRegistry, Alert, FlatList, StyleSheet, Text, View } from 'react-native'; export default class FlatListBasics extends Component { _onscroll() { this.flatList.scrollToEnd( { animated: false } )

Vector<int> factory with initialization doesn't work

i work for buck I'm trying to create a vector factory that creates a vector of values 1with a specified size , sbut it's not working. template<size_t s> constexpr std::vector<int>& vector_factory() { std::vector<int> v(s, 1); return v; } int main(int

Vector<int> factory with initialization doesn't work

i work for buck I'm trying to create a vector factory that creates a vector of values 1with a specified size , sbut it's not working. template<size_t s> constexpr std::vector<int>& vector_factory() { std::vector<int> v(s, 1); return v; } int main(int

Why doesn't const work even though the variable hasn't changed?

Roberto Rocco I am trying to protect the -a-array of my_sum_array function from changes. In the function block, I didn't make any changes to -a-, but I have a warning (WARNING: Assigning from 'const int *' to 'int *' discards qualifiers [-Wincompatible-pointer

Why doesn't const work even though the variable hasn't changed?

Roberto Rocco I am trying to protect the -a-array of my_sum_array function from changes. In the function block, I didn't make any changes to -a-, but I have a warning (WARNING: Assigning from 'const int *' to 'int *' discards qualifiers [-Wincompatible-pointer

Why doesn't const work even though the variable hasn't changed?

Roberto Rocco I am trying to protect the -a-array of my_sum_array function from changes. In the function block, I didn't make any changes to -a-, but I have a warning (WARNING: Assigning from 'const int *' to 'int *' discards qualifiers [-Wincompatible-pointer

Why doesn't const work even though the variable hasn't changed?

Roberto Rocco I am trying to protect the -a-array of my_sum_array function from changes. In the function block, I didn't make any changes to -a-, but I have a warning (WARNING: Assigning from 'const int *' to 'int *' discards qualifiers [-Wincompatible-pointer

Why doesn't const work even though the variable hasn't changed?

Roberto Rocco I am trying to protect the -a-array of my_sum_array function from changes. In the function block, I didn't make any changes to -a-, but I have a warning (WARNING: Assigning from 'const int *' to 'int *' discards qualifiers [-Wincompatible-pointer

Comparing variable to `int` doesn't work

Matt Johnson My program requires the user to provide an integer, so I'm trying to create a loop that if they enter a non-integer, the loop doesn't end until an integer is entered. I tried: PlayerCount = input("How many players?") while PlayerCount != int:

Comparing variable to `int` doesn't work

Matt Johnson My program requires the user to provide an integer, so I'm trying to create a loop that if they enter a non-integer, the loop doesn't end until an integer is entered. I tried: PlayerCount = input("How many players?") while PlayerCount != int:

Why doesn't the CustomStringConvertible protocol work with Int?

tailor public func +<T: CustomStringConvertible>(lhs: T, rhs: T)->String{ return lhs.description+rhs.description } let a:String = "A" let i:Int = 0 print(a+i) I am overloading the '+' operator for the CustomStringConvertible type. Both String and Int c

Why doesn't the CustomStringConvertible protocol work with Int?

tailor public func +<T: CustomStringConvertible>(lhs: T, rhs: T)->String{ return lhs.description+rhs.description } let a:String = "A" let i:Int = 0 print(a+i) I am overloading the '+' operator for the CustomStringConvertible type. Both String and Int c

Not sure why my variable doesn't work

Stewart I had variables "ballX" and "ballY" before. However, when I try to use console.log to view their values, they now show as "undefined", so I must be messing with them. I've spent 2-3 hours going through my code and looking online for fixes to no avail,