0

My program is a list of cities, it's good but when I call to popFront(), I don't know why but it doesn't work, then I call any function and the program is over.

City* popFront(Nodo*& header, Nodo*& trailer) {
    City* libras;
    if (inicio) {
        strcpy(libras->Name,inicio->dato.Name );
        libras->Num = header->dato.Num;
        Nodo* aux = header;
        header= header->next;
                header->previous= NULL;
        delete aux;
        if (!header) trailer = NULL;
    }
    return libras;
}
void read(Nodo*& head) {    
    Nodo* aux = head;
    int pos = 1;
    while (pos <= node_count) {
        cout << "pos" << pos << endl;
        cout << "Name> " << aux->dato.Name << endl;
        cout << "NUm> " << aux->dato.Num << endl;
        aux = aux->next;
        pos++;
    }
    if (node_count == 0)cout << "Empty list" << endl;
}

1 Answers1

0

Well, I'm not sure this is the only problem, but this right here is wrong:

City* libras;

You need to allocate it before you use it, like this:

City* libras = new City;

Otherwise, something like strcpy(libras->Name,inicio->dato.Name ); will fall and crash hard. That's because it's UB (Undefined Behavior) to access a pointer to memory that is unallocated.

Of course, you will need to delete it when you're done with it, but you seem to know and understand that already based on your other code:

delete libras; // Or wherever the returned pointer gets stored