Segmentation fault when printing an array in C ++

1

I'm having problems printing an array in C ++. If I do what I indicate in the code below, you can access the data of each object in the array, therefore print:

void testGetVehiculos(){
    string ci = "c11";
    int cantVehiculos=2;

    int pos = buscarSocio(ci); //devuelve la posición del socio con documento ci en un array global
    vector<DtVehiculo*> vehiculos = socios[pos]->getVehiculos(ci, cantVehiculos);

    DtVehiculo** vehiculos = vehiculos.data();//obtenerVehiculos(ci, cantVehiculos);
    string marca = "";  
    cout << " \n";
    cout << "Se encontraron: " << cantVehiculos << " vehiculos.\n";
    cout << " \n";
    cout << "Listado: \n";
    for (int i = 0; i < cantVehiculos ; i++){
            //DtVehiculo  m = vehiculos[i];
    //        DtVehiculo* c = cons[i];
            marca = vehiculos[i]->getMarca(); 
            cout << " \n";
            cout << "------------------ \n";
            cout << "-> Número de vehiculo: " << i<< "\n";
            cout << "-> Marca: " << marca << "\n";
            cout << "------------------ \n";

    }

    cout << "-------FIN-----\n";
}

If I put in a separate function (which is what you are asked to do), when you reach the line:

marca = vehiculos[i]->getMarca(); 

I get Segmentation Fault. Here the complete code that gives problems:

DtVehiculo** obtenerVehiculos(string ci, int& maxVehiculos){
    int pos = buscarSocio(ci); //devuelve la posición del socio con documento ci en un array global
    vector<DtVehiculo*> vehiculos = socios[pos]->getVehiculos(ci, maxVehiculos);// obtiene la lista de Vehiculos de la clase Socio. Hasta acá  parece funcionar todo bien
    return vehiculos.data();
};


void testGetVehiculos()
{
    string ci = "c11";
    int cantVehiculos=2;

    DtVehiculo** vehiculos = obtenerVehiculos(ci, cantVehiculos);
    string marca = "";  
    cout << " \n";
    cout << "Se encontraron: " << cantVehiculos << " vehiculos.\n";
    cout << " \n";
    cout << "Listado: \n";
    for (int i = 0; i < cantVehiculos ; i++){
            //DtVehiculo  m = vehiculos[i];
    //        DtVehiculo* c = cons[i];
            marca = vehiculos[i]->getMarca(); 
            cout << " \n";
            cout << "------------------ \n";
            cout << "-> Número de vehiculo: " << i<< "\n";
            cout << "-> Marca: " << marca << "\n";
            cout << "------------------ \n";

    }

    cout << "-------FIN-----\n";
}

Thank you in advance.

    
asked by Mathias 25.03.2018 в 14:49
source

1 answer

2

Let's look at your problem function :

int pos = buscarSocio(ci); //devuelve la posición del socio con documento ci en un array global

Ok, that looks good. Let's continue:

vector<DtVehiculo*> vehiculos = socios[pos]->getVehiculos(ci, maxVehiculos);// obtiene la lista de Vehiculos de la clase Socio. Hasta acá  parece funcionar todo bien

Here there are already weird things: you create an automatic variable (or, which is the same, temporary ).

return vehiculos.data( );

And here it is confirmed: you return a data contained in the variable temporary above.

In theory, there should be no problems ... until we document what the function does std::vector< >::data( ) :

  

Returns pointer to the underlying array serving as element storage. The pointer is such that range [data (); data () + size ()) is always a valid range, even if the container is empty (data () is not dereferenceable in that case).

Okay. The value returned by data( ) points to the internal memory of the std::vector ... that we remember was automatic. What will happen to that memory, when the vector is destroyed? Indeed, that the memory is released ... and the value previously obtained when calling data( ) is no longer valid .

One possible solution would be to return the std::vector< > directly:

std::vector< DtVehiculo * > obtenerVehiculos( string ci, int & maxVehiculos ) {
  int pos = buscarSocio( ci );
  return socios[pos]->getVehiculos( ci, maxVehiculos );
};

And a minimum change within your testGetVehiculos( ) :

auto vehiculos = obtenerVehiculos( ci, cantVehiculos );

With that, it should work.

    
answered by 25.03.2018 / 19:31
source