vectors of pointers

This is a discussion on vectors of pointers within the C++ Programming forums, part of the General Programming Boards category; I am getting memory leak errors when running my program.. I have determined that the error is in this section, ...

  1. #1
    Registered User Myownworstenemy's Avatar
    Join Date
    May 2003
    Posts
    8

    vectors of pointers

    I am getting memory leak errors when running my program.. I have determined that the error is in this section, specifically in the push_back line for the vector. the newCard variable receives the pointer to the Card object just fine. The problem occurs when I try to push this pointer onto the vector. the vector is part of the class this funtion belongs to,a nd is defined as vector <Card*> guardiansHand. Any input or information would be appreciated. thanks!

    Code:
    		for ( int x = 0; x < drawNumber; x++ )
    		{
    			Card* newCard = new Card;
    			cardIndex = ( rand( ) % theDeck->getDeckSize( ) + 1 );
    
    			cout << cardIndex << endl;
    
    			theDeck->findACard( cardIndex );
    
    			newCard = theDeck->findACard( cardIndex );
    
    			guardiansHand.push_back( newCard );
    
    			newCard->printCardInfo( );
    
    		}

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    You create a new Card object and assign that to the pointer newCard but then you later reassign this pointer to the value returned from the theDeck->findACard( cardIndex ) function call and push this new value onto the vector. What happens to the memory that you got from the original call to new?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User Myownworstenemy's Avatar
    Join Date
    May 2003
    Posts
    8
    That is a valid point, and I have to admit that I don't completely understand pointers... in that previous code example, I allocated space for the newCard pointer, and then assigned the data in a different pointer to the newCard pointer, and deleted the old pointer. (this is done behind the scenes of the code section I have shown) However, the problem is identical when I try to use only the call to the findACard( ) function (which returns a pointer to a Card object) and don't deal at all with the newCard pointer.

    It is possible I didn't completely understand your question, but I feel the problem lies in a diff. direction.

    Code:
    for ( int x = 0; x < drawNumber; x++ )
    		{
    			//Card* newCard = new Card;
    			cardIndex = ( rand( ) % theDeck->getDeckSize( ) + 1 );
    
    			cout << cardIndex << endl;
    
    
    			//newCard = theDeck->findACard( cardIndex );
    
    			guardiansHand.push_back( theDeck->findACard( cardIndex ) );
    
    			//newCard->printCardInfo( );
    }

  4. #4
    Registered User Myownworstenemy's Avatar
    Join Date
    May 2003
    Posts
    8
    Another note on that issue is that the program works perfectly when I use the newCard pointer by itself, and simply output the value to the screen. The problem occurs when i try to push the newCard value onto the vector.

  5. #5
    Registered User Myownworstenemy's Avatar
    Join Date
    May 2003
    Posts
    8
    Here is the other function referenced in my code...



    Code:
    	void Hand::drawCards( Deck* theDeck, int drawNumber )
    	{
    		static int init = 1;
    		int cardIndex;
    
    		if ( init == 1 )
    		{
    			srand( time( NULL ) );
    		}
    
    		for ( int x = 0; x < drawNumber; x++ )
    		{
    
    			cardIndex = ( rand( ) % theDeck->getDeckSize( ) + 1 );
    
    			cout << cardIndex << endl;
    
    			guardiansHand.push_back( theDeck->findACard( cardIndex ) );
    
    
    		}

    and the findACard( ) function in the Deck class:


    Code:
    	Card* Deck::findACard( int cardIndex )
    	{
    		return guardiansDeck[ cardIndex ];
    	}

    guardiansDeck is assigned with vector<Card*> guardiansDeck; it is a private member of Deck. findACard is basically just a "getter" function for a specific item within a vector of Card pointers. The purpose of the drawCard function is to get a pointer from within the Deck vector, and push it onto the Hand vector. Since they are both pointers to the same location in memory, I shouldn't need to reallocate the memory, right?

    If i just comment out the line that pushes the pointer onto the vector, it runs perfectly...
    I can put in a line, such as



    Code:
    Card* newCard = theDeck->findACard( cardIndex );

    and everything works fine. It only encounters problems when i push it onto the vector.

    As far as deleting pointers, and freeing memory... I do have functions to do that once I am done with the pointer... but the program doesnt even get that far...

  6. #6
    Registered User Myownworstenemy's Avatar
    Join Date
    May 2003
    Posts
    8
    In case anyone reads this in the future and wants to help, I already solved the problem. Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers and vectors
    By larne in forum C Programming
    Replies: 6
    Last Post: 09-12-2008, 10:27 AM
  2. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  3. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  4. Replies: 4
    Last Post: 12-10-2006, 06:08 PM
  5. Points, vectors, matrices
    By subnet_rx in forum Game Programming
    Replies: 17
    Last Post: 01-11-2002, 01:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21