You are here:

C++/trouble preserving objects - pointers to objects become invalid

Advertisement


Question
   I am having trouble getting objects to be preserved outside of a loop that reads data from a file and then imputs that data into objects.

Here is the code that is responsible for the input.

       char name[MAX];
     char streetAddress[MAX];
     char city[MAX];
     char state[MAX];
     char zip[MAX];
     char emailAddress[MAX];   
     char BizName[MAX];
     char Birthday[MAX];
     char relationship[MAX];
     char temp0[MAX];
     char temp1[MAX];
     char temp2[MAX];
     char junk;
     int type;
     
     ifstream infile;
     infile.open ("DataBase.txt");
     
     while ( infile.good() )
     {
        infile >> type;
        cout << endl << "type=" << type << endl;  
        
        infile >> junk;
        if ( infile.eof() )
         break;
        
        else if (type == FAMID )
        {
         cout << endl << "begin read of Family person" << endl;
         infile.getline (name,MAX,'$');
         cout << endl << "name:" << name << endl;
         infile.getline (streetAddress,MAX,'$');
         cout << endl << "streetAddress:" << streetAddress << endl;
         infile.getline (city, MAX, '$' );
         cout << endl << "city;" << city << endl;
         infile.getline (state, MAX, '$' );
         cout << endl << "state;" << state << endl;
         infile.getline (zip, MAX, '$' );
         cout << endl << "zip;" << zip << endl;
         infile.getline (temp0, MAX, '$' );
         cout << endl << "temp0;" << temp0 << endl;
         infile.getline (temp1, MAX, '$' );
         infile.getline (temp2, MAX, '$' );
         infile.getline (emailAddress, MAX, '$' );
         infile.getline (Birthday, MAX, '$' );
         infile.getline (relationship, MAX, '$' );
         
         Family FamPerson;
         
         FamPerson.changeName ( name ) ;  
         FamPerson.changeStreetAddress ( streetAddress);
         FamPerson.changeCity ( city ); //
         FamPerson.changeState ( state ); //
         FamPerson.changeZip ( zip );     // change form default
         FamPerson.changePhoneNum ( 0, temp0 );   // change phone num from default
         FamPerson.changePhoneNum ( 1, temp1 );
         FamPerson.changePhoneNum ( 2, temp2 );
         FamPerson.setEmailAddress ( emailAddress ); //
         FamPerson.setBirthday ( Birthday );
         FamPerson.setRelationship ( relationship );
         
         personinfo *personinfoptr2 = &FamPerson;
         
         cout << endl << endl << "begin Famperson display inside the readfile----------------------" << endl;  
         FamPerson.display();
         
         newPhoneBookObj.insert ( personinfoptr2 );
         //MapOfPhoneBookData.insert (make_pair( personinfoptr2->getName() , personinfoptr2 ));   
         
         
        }
        else if ( infile.eof() )
         break;
        
        else if (type == FAMFRIID )
        {
         cout << endl << "begin read of Family and Friend person" << endl;
         infile.getline (name,MAX,'$');
         cout << endl << "name:" << name << endl;
         infile.getline (streetAddress,MAX,'$');
         infile.getline (city, MAX, '$' );
         infile.getline (state, MAX, '$' );
         cout << endl << "state:" << state << endl;
         infile.getline (zip, MAX, '$' );
         cout << endl << "zip;" << zip << endl;
         infile.getline (temp0, MAX, '$' );
         cout << endl << "temp0:" << temp0 << endl;
         infile.getline (temp1, MAX, '$' );
         cout << endl << "temp1:" << temp1 << endl;
         infile.getline (temp2, MAX, '$' );
         cout << endl << "temp2:" << temp2 << endl;
         infile.getline (emailAddress, MAX, '$' );
         cout << endl << "emailAddress:" << emailAddress << endl;
         infile.getline (Birthday, MAX, '$' );
         cout << endl << "Birthday:" << Birthday << endl;
         
         FamAndFriends Person;
         Person.changeName ( name ) ;  
         Person.changeStreetAddress ( streetAddress);
         Person.changeCity ( city ); // change from default to springfeild
         Person.changeState ( state ); // change from default
         Person.changeZip ( zip );     // change form default
         Person.changePhoneNum ( 0, temp0 );   // change phone num from default
         Person.changePhoneNum ( 1, temp1 );
         Person.changePhoneNum ( 2, temp2 );
         Person.setEmailAddress ( emailAddress ); // change from default to fake@fakewakey.com
         Person.setBirthday ( Birthday );
         
         cout << endl << endl <<"begin FamAndFriends display inside the readfile----------------------" << endl;  
         Person.display();
         
         personinfo *personinfoptr2 = &Person;
         newPhoneBookObj.insert ( personinfoptr2 );
        // MapOfPhoneBookData.insert (make_pair( personinfoptr2->getName() , personinfoptr2 ));   
         
        }


----------------------------

that is only part of the loops that pull the data from a file.
newPhoneBookObj.insert ( personinfoptr2 );

Is a function of newPhoneBookObj that does bascally what:

MapOfPhoneBookData.insert (make_pair( personinfoptr2->getName() , personinfoptr2 ));   

would do.  

the problem is that basacally the object pointed to falls out of scope and the pointer becomes invalid.  


I'm probibly making this question to complicated,
bascally I just need to know how to keep the object that is being pointed to from going out of scope and therefore making the pointer invalid.
I tried using new to make a NTO and then push data into that like:

personinfoptr2 -> changeName (name);  
then pushing the pointer into the multimap but when it came to the derived class overridden functions it would give an error:
driver.cpp:287: error: no matching function for call to 'personinfo::
setBirthday(char[300])'
personinfo.cpp:177: error: candidates are: virtual void
personinfo::setBirthday()


I am running minGW complier.
Any help would be great :) let me know if you need more info.  

Answer
Hello Nathan, thanks for the question.

Yeah, the problem being that when you store the pointer to that local object, it turns to undefined data when that object falls out of scope. The way to get around that is to allocate your pointer with "new". Then if you want it to store all the data you can simply "memcpy" all of the old data over to it, to knock that out with one line of code. Here's an example:

personinfo *personinfoptr2 = new FamAndFriends;

memcpy(personinfoptr2, &Person, sizeof(FamAndFriends));

You can then push it back onto whatever kind of data structure newPhoneBookObj is. There is a problem here though. I have no idea how your inheritence is working. I'm making the assumption that since you were having a personinfo* point at the address of a FamAndFriends object, that inheritence was used, or it's a friend class, or something of the like. If that's not the case, I'm sure we can figure something out with a void*. I usually like to try the simplest things first though.

I hope this information was helpful.

- Eddie

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Eddie

Expertise

I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by ExpertRating.com. I also know a good deal about graphics via OpenGL, and GUIs.

Experience

I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2016 About.com. All rights reserved.