Hypofreak
Hypofreak

Reputation: 13

C++ Aborted (Core Dumped) when returning a vector from a function

I'm trying to return a vector from a function. My code compiles and I've checked my function and reckon that the error comes from the return part. It compiles fine (using Cygwin) but when running it, I get an Aborted (core dumped) error. Here is my code:

#include <iostream>
#include <vector>
#include <string>
using namespace std;

//function that returns the square
int f(int n)
{
    return n*n;
}
vector<int> myVec;
int counter = 0;
//function that uses f on all elements in a list
vector<int> map(vector<int> something)
{
    //base case
    if(counter == something.size())
    {
        /*cout << "hello" << endl;
        for (int i=0; i<counter; i++)
        {
            cout << "vector: " << myVec[i] << endl;
        }*/
        counter=0;
        return myVec;
    }
    //recursion
    else
    {
        //cout << "counter: " << counter << endl;
        int n = f(something[counter]);
        //cout << "n: " << n << endl;
        myVec.push_back(n);
        //cout << "vector: " << myVec[counter] << endl;
        counter++;
        map(something);
    }
}

int main()
{
    //making vectors
    vector<int> L;
    vector<int> L1;
    vector<int> L2;
    for (int i=0; i<20; i++)
    {
        L.push_back(i);
    }
    L1 = map(L);
}

The code was originally from a class file.

Upvotes: 1

Views: 1225

Answers (2)

Zinki
Zinki

Reputation: 446

In your recursion, you do not return anything. The function is expected to return a Vector.

In your case, what happens if the function enters the "else" case on its first call? It reenters map() until the condition is met, then returns a vector. That vector is passed to the previous recursive call and immediately deleted, as it is not passed any further.

The solution here would be to change the last line of the else-case to

return map(something);

so the value is not lost and correctly passed through to the original caller (your main function).

Upvotes: 1

The Quantum Physicist
The Quantum Physicist

Reputation: 26276

ALWAYS return a vector when your return type is a vector. In your function, there's a branch that won't return anything, and that will cause problems.

vector<int> map(vector<int>& something)
{
    //base case
    if(counter == something.size())
    {
        /*cout << "hello" << endl;
        for (int i=0; i<counter; i++)
        {
            cout << "vector: " << myVec[i] << endl;
        }*/
        counter=0;
        return myVec;
    }
    //recursion
    else
    {
        //cout << "counter: " << counter << endl;
        int n = f(something[counter]);
        //cout << "n: " << n << endl;
        myVec.push_back(n);
        //cout << "vector: " << myVec[counter] << endl;
        counter++;
        map(something); //you should return a vector here
        return std::vector<int>(); //empty vector
    }
}

Also notice the "&" symbol I added at the function call so that the vector is passed by reference. Otherwise you're passing a copy that won't be changed. I don't know what that "map" function does, so I can't suggest better models to what you're doing.

Upvotes: 1

Related Questions