mardi 20 avril 2021

pointer returned to an object turns null after function returns address

I am new to CPP and I am writing a program as an assignment to simulate a train path system that includes destinations and starts using object oriented programming . I have 2 classes as shown below (there is a a passenger class but it is not relevant ) :


     class Train
    {
    public:
         int cooldown_time;
         int travel_time;
         int time_since_movement;
         int id;
         class Station *start;
         class Station *destination;
         vector<Passenger *> current_passengers;
         string status;
         void add_train(vector<string> commands, vector<Station> stations, vector<Train> &trains)
         {
              travel_time = stoi(commands[THIRD_PART + 1]);
              cooldown_time = stoi(commands[THIRD_PART + 2]);
              status = TSTATUS1;
              start = station_search(stations, commands[SECOND_PART]); // this is where the problem happens
              destination = station_search(stations, commands[THIRD_PART]);
              id = stations.size();
         }
    };
    class Station
    {
    public:
         int tuffy_price;
         string city_name;
         vector<Passenger *> current_passengers;
         vector<Train *> current_trains;
         int id;
         void add_station(vector<Station> &stations, vector<string> &commands)
         {
              tuffy_price = stoi(commands[THIRD_PART]);
              city_name = commands[SECOND_PART];
              id = stations.size();
         }
    };

I have a search function dedicated to finding the start and destination based off a command that user enters for example :the user enters "add_train cityname1 cityname2 <cooldown_time> <travel_time>". my program detects the city names and searches a vector I have named stations with a key that is the city name and returns a pointer (because of the complications in memory behavior in a function , i set it to pointer) to that station-object . the function is as below :

Station *station_search(vector<Station> stations, string key)
    {
         Station *dummy;
         for (int i = 0; i < stations.size(); i++)
         {
              if (stations[i].city_name == key)
              {
                   return &stations[i];
              }
         }
         return dummy;
    }} 

my problem is with my search function's weird behavior , when I debug the program I see the function find the correct station object and return a pointer to it ,but when the execution returns to the constructor function it randomly (maybe not randomly ) turns the first pointer relating to the start station to null and replaces the values inside with garbage ones. but after the function searches for the destination station it does not do this and the execution is correct.

Could someone explain why this error is occurring? My guess is that I have not understood local variables and pointer returns well enough and I have committed a rookie mistake somewhere but I don't seem to find it .

PS: I did not include the full code as it's too long I can include it by attaching a file ,comment down if it's necessary.

Aucun commentaire:

Enregistrer un commentaire