Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C/C++ - Violazione di memoria liste..
Forum - C/C++ - Violazione di memoria liste..

Avatar
Lego86 (Member)
Rookie


Messaggi: 30
Iscritto: 04/10/2008

Segnala al moderatore
Postato alle 11:35
Sabato, 05/03/2011
Ciao a tutti!
Ho scritto un programma che carica una lista con dei nomi e delle età. Il programma dovrebbe stampare l'elemento con età maggiore, ma quando provo a stampare a video o a fare il controllo dell'età sui singoli nodi mi dà un errore di memoria.. potreste darmi una mano? :(

Grazie mille,
Roberto

main.cpp
Codice sorgente - presumibilmente C++

  1. #include <cstdlib>
  2. #include <iostream>
  3. #include "header.h"
  4. using namespace std;
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8.     pNodo testa;
  9.     inizializzaLista();
  10.     testa=inserisciInLista(testa);
  11.     stampaLista(testa);
  12.     trovaMassimo(testa);
  13.     system("PAUSE");
  14.     return EXIT_SUCCESS;
  15. }



header.h
Codice sorgente - presumibilmente C++

  1. #ifndef ESAMELISTA_H
  2. #define ESAMELISTA_H
  3. #define N 100
  4. typedef char stringa[N];
  5. typedef struct nodo *pNodo;
  6. typedef struct nodo{
  7.    stringa nome;
  8.    int eta;
  9.    pNodo next;
  10. };
  11.  
  12. pNodo inizializzaLista();
  13. pNodo push(pNodo,stringa,int);
  14. pNodo inserisciInLista(pNodo);
  15. void trovaMassimo(pNodo);
  16. void stampaLista(pNodo);
  17. #endif



header.cpp
Codice sorgente - presumibilmente C++

  1. #include <iostream>
  2. #include "header.h"
  3. using namespace std;
  4.  
  5. pNodo inizializzaLista(){
  6.    pNodo p=new nodo;
  7.    p=0;
  8.    return p;
  9. }
  10.  
  11. pNodo push(pNodo testa,stringa name,int age){
  12.    pNodo p=new nodo;
  13.    strcpy(p->nome,name);
  14.    p->eta=age;
  15.    p->next=testa;
  16.    return p;
  17. }
  18. pNodo inserisciInLista(pNodo testa){
  19.    stringa name;
  20.    int n,age;
  21.    cout<<"Quante persone vuoi inserire nella lista?"<<endl;
  22.    cin>>n;
  23.    for(int i=0;i<n;i++){
  24.       cout<<"Inserici nome "<<i+1<<": ";
  25.       cin>>name;
  26.       cout<<"Inserici eta "<<i+1<<": ";
  27.       cin>>age;
  28.       testa=push(testa,name,age);
  29.    }
  30.    return testa;
  31. }
  32.  
  33. void trovaMassimo(pNodo testa){
  34.    int maxEta=0;
  35.    string maxNome;
  36.    while(testa!=0){
  37.    int listAge=testa->eta;
  38.      if(maxEta<(listAge)){
  39.          maxEta=testa->eta;
  40.          maxNome=testa->nome;
  41.       }
  42.       testa=testa->next;  
  43.    }
  44.    cout<<"La persona piu' grande e' "<<maxNome<<" di anni "<<maxEta<<endl;
  45. }
  46.  
  47. void stampaLista(pNodo testa){
  48.    cout<<"La lista e':"<<endl;
  49.    while(testa!=0){
  50.       cout<<testa->nome<<endl;
  51.       cout<<testa->eta<<endl;
  52.       testa=testa->next;
  53.    }
  54. }


PM Quote
Avatar
lorenzo (Normal User)
Guru


Messaggi: 1178
Iscritto: 15/04/2008

Segnala al moderatore
Postato alle 17:42
Sabato, 05/03/2011
semplice, hai dimenticato il valore di ritorno nel main:

Codice sorgente - presumibilmente C++

  1. #include <cstdlib>
  2. #include <iostream>
  3. #include "header.h"
  4. using namespace std;
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8.     pNodo testa;
  9.     testa = inizializzaLista(); //IL VALORE DI RITORNO
  10.     testa=inserisciInLista(testa);
  11.     stampaLista(testa);
  12.     trovaMassimo(testa);
  13.     system("PAUSE");
  14.     return EXIT_SUCCESS;
  15. }


PM Quote
Avatar
Lego86 (Member)
Rookie


Messaggi: 30
Iscritto: 04/10/2008

Segnala al moderatore
Postato alle 10:13
Domenica, 06/03/2011
Che cretino.. grazie mille!:k:

PM Quote