Thread: Pointer usage problem.

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148

    Pointer usage problem.

    Hi,

    I am after some guidance on how to solve my problem.

    I've wrote some example code to try and help explain it:


    Code:
    for(;;) 
    { 	
        cout << "Menu options:" << endl; 	
        cout << "1) Enter a new person" << endl; 	
        cout << "2) Save and Exit" << endl;   	
    
        switch(choice) 
          case 1:  	
             cout << "Enter the person's name" << endl; 	
             cin >> name;  	
    
             Person *ptr = new Person(name);  	
             v.push_back( *ptr );//Declaration ==  vector<Person*> v;   
    
          case 2: 	
             //Save all the Person objects to a text file for future use. 	
             //Exit the program. 
    }
    As you can see, the program will be repeatedly asking the user for a person's name and then storing the details in a vector.

    My confusion is what happens when more than one pointer/object is created.

    The program will be trying to make a second pointer with the same name (ptr).

    If the user goes onto create 10 people, that will be 10 *ptr's.

    So I was wondering how this is normally handled.

    I'm trying to hold all the Persons in the vector, so that they can be searched through, and once the user wishes to exit the program, save them all to a text file.

    Thanks for any help, it's appreciated.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    A variable is destroyed at the end of the block it is defined in. So when you reach the end of the for loop (and before re-starting the for loop) your local variables (including ptr) are destroyed. Now you've still got a pointer to your person stored in your vector, so you haven't lost that bit of memory, but the local variable is gone.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    Oh really?!?! So this is a non issue? That's great. Embarrassed I didn't know, but great to learn it's not a problem as it is.


    Thanks very much tabstop, real appreciated!

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    You also have a memory leak. Always remember to 'delete' what you 'new'.

  5. #5
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    Thanks Sebastiani.

    This is to delete the actual object I believe.

    I forgot that.


  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Actually, that code can't work if v is a vector<Person*> because ptr is a Person* and *ptr is just a Person. The types don't match.

    If v is actually a vector<Person>, then as Sebastiani says you have a memory leak. Of course, if that's the case, there's no reason to use new there anyway, just do:
    Code:
             v.push_back( Person(name) );//Declaration ==  vector<Person> v;
    However, if v really is a vector<Person*>, then you need to remember to delete the memory later by looping through the vector and deleting each item. Of course, unless you are using polymorphism or just practicing with pointers, you'd probably want to prefer vector<Person>.

  7. #7
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    Hi Daved, thnaks for your valuable input.

    It is vector<Person*> in the program.

    There will be a few classes all derived from a single Person base class.

    This is why I'm storing pointers to the objects in the vector instead of the objects themselves.

    I hope that makes sense, I'm a little confused about the meaning of this right now:

    Actually, that code can't work if v is a vector<Person*> because ptr is a Person* and *ptr is just a Person. The types don't match.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It means just what it says: your vector is a vector of Person*, but *ptr is not a Person* and therefore cannot be added to the vector. ptr is a Person*, so maybe that's what you intended to add instead.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> There will be a few classes all derived from a single Person base class.
    Ok. If Person is a base class, then you are using polymorphism, and holding a Person* makes sense. Normally, you'd be adding pointers to the derived class objects to the vector later. For example, if Student and Teacher were classes derived from Person, then you might do this:
    Code:
             Student *pStudent = new Student(name);
             v.push_back( pStudent );//Declaration ==  vector<Person*> v;
             // ...
             Teacher *pTeacher = new Teacher(name);
             v.push_back( pTeacher );//Declaration ==  vector<Person*> v;
    As far as the quote you were confused about, you have to pay attention to the types. So your vector holds Person*. That means you have to pass a pointer to a person to the push_back function. In your code, ptr is a pointer to a Person, so push_back( ptr ) would be correct.

    However, you had push_back( *ptr ). That * changes the ptr from a Person* to just a Person. Those are two different types, so the example code won't compile. I assume in your real code you have it right if you have been able to get it to compile.

  10. #10
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    Thanks tabstop.

    I need to edit my previous post to this:

    //-----------------------

    I am using vector<Base> in the program.

    There will be a few classes all derived from a single Base class.

    I shall be storing pointers in the vector to avoid slicing:


    Code:
    Base *ptr = new Base(param1, param2);
    
    v.push_back( *ptr );
    I hope this looks correct to you guys.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Swerve View Post
    Thanks tabstop.

    I need to edit my previous post to this:

    //-----------------------

    I am using vector<Base> in the program.

    There will be a few classes all derived from a single Base class.

    I shall be storing pointers in the vector to avoid slicing:


    Code:
    Base *ptr = new Base(param1, param2);
    
    v.push_back( *ptr );
    I hope this looks correct to you guys.
    You started off at about 90% correct. Unfortunately you are now down to 10% correct. If you want to avoid slicing, you should store pointers -- so that means
    1. your vector needs to be of type vector<Base *> so that you can actually store pointers
    2. your allocation needs to be of the type you want; i.e. you need
      Code:
      Base *ptr = new Derived(param1, param2);
      to even have a derived object in the first place
    3. if you are going to store pointers, then you need to store pointers. ptr is a pointer. *ptr is emphatically NOT a pointer.

  12. #12
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    Thanks to everyone for helping me out with this.

    I have it sorted now and also I understand it all.

    Once again you guys come to the rescue.

    bigthumbsup.jpg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer to pointer realloc problem
    By prakash0104 in forum C Programming
    Replies: 14
    Last Post: 04-06-2009, 08:53 PM
  2. Pointer problem
    By mikahell in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2006, 10:21 AM
  3. A homework problem about C++ (Pointer)
    By joenching in forum C++ Programming
    Replies: 10
    Last Post: 03-14-2005, 04:28 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM