error : pointer being freed not allocated

This is a discussion on error : pointer being freed not allocated within the C++ Programming forums, part of the General Programming Boards category; I have typed this code from a book after learning about classes/objects in c++. It runs until the user enters ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    51

    error : pointer being freed not allocated

    I have typed this code from a book after learning about classes/objects in c++.
    It runs until the user enters name for player 1, but then the error
    "Conquest game classes objects(491) malloc: *** error for object 0x100007280: pointer being freed was not allocated
    *** set a breakpoint in malloc_error_break to debug"

    I dont see any pointers used in this code, nor any memory allocated with "new"(which are both covered in the next chapter of the book).

    Is there a problem with the code from the book, or have I made a silly mistake. I have tried going over the code looking for typing errors, but can't see any, also I tried to solve the malloc / pointer not freed problem with google searches but couldn't see how this code used them.

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    //handles each nation for each player
    class Nation{
    public:
    	int land;
    	int troops;
    private:
    	string name;
    	int food;
    	int gold;
    	int people;
    	int farmers;
    	int merchants;
    	int blacksmiths;
    public:
    	Nation (string lName);
    	Nation();
    	~Nation();
    	bool takeTurn();
    private:
    	void menu(void);
    };
    
    Nation nation1;
    Nation nation2;
    
    //sets the default nation values
    Nation::Nation(string lName):
    	name(lName),land(20), food(50), troops(15), gold(100), people(100),
    farmers(0), merchants(0), blacksmiths(0){}
    
    //a default constructor
    Nation::Nation(){}
    
    //takes a turn for player
    bool Nation::takeTurn(){
    	cout<<"It's now" <<name<< "'s turn.\n";
    	people += land * 0.2;
    	food += farmers - people * 0.25;
    	gold += merchants * 20;
    	troops += blacksmiths;
    	menu();
    	if (nation1.land <= 0 || nation2.land<=0) {
    		return false;
    	}//if
    	return true;
    }//bool nation
    
    void Nation::menu(){
    	while (true) {
    		int input = 0;
    		cout << "Food " << food<< "\nGold"<<gold
    		<<"\nland "<<land<<"\nMerchants "<< merchants<<"\nTroops "
    		<<troops<<"\nUnemployed "<< people<<"\n";
    		cout<<"1) Buy Land.\n"
    			<<"2) Hire Farmers\n"
    			<<"3) Hire merchants\n"
    			<<"4) Hire Weaponsmiths\n"
    			<<"5) Attack!\n"
    			<<"6)Take Turn\n";
    		cin>>input;
    		switch (input) {
    			case 1://buys land
    				cout << "You buy "<<gold/20<<" sections of land\n";
    				land += gold/20;
    				gold %=20;
    				cout<< "You now have "<<gold<< "gold\n";
    				
    				break;
    				
    				case 2:
    				farmers += people;
    				cout<<"you hired " << people<< " farmers\n";
    				people = 0;
    					break;
    				
    				case 3:
    				merchants += people;
    				cout<< "You hired " << people<< "merchants\n";
    				people = 0;
    					break;
    				
    				case 4:
    				blacksmiths += people;
    				cout<< "You hired " << people<< "blacksmiths\n";
    				people = 0;
    				break;
    					
    				case 5:
    					cout<< "the war wages into the night and all die\n";
    				if (nation1.troops < nation2.troops) {
    					nation2.land += 10;
    					nation1.land -= 10;
    				}//if
    				
    				else if (nation1.troops > nation2.troops){
    					nation2.land -= 10;
    					nation1.land += 10;
    				}//else if
    				
    				nation1.troops = 0;
    				nation2.troops = 0;
    				break;
    				
    				case 6:
    				return; //ends the turn
    					break;
    
    
    		}//while
    		
    	}//switch
    }//menu
    
    
    
    
    
    
    int main(){
    	string tempString;
    	cout<<"Welcome to the conquest\n";
    	cout<<"What is your name player 1?\n";
    	cin>>tempString;
    	nation1 = Nation(tempString);
    	
    	
    	cout<<"What is your name player 2?\n";
    	cin>>tempString;
    	nation2 = Nation(tempString);
    	while (nation1.takeTurn() && nation2.takeTurn()) {
    		
    	}
    	return 0;
    }//main

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    Which OS/Compiler are you using?

    Apart from adding
    Code:
    Nation::~Nation(){}
    it seems find to me.


    Code:
    $ g++ --version
    g++ (Ubuntu 4.3.3-5ubuntu4) 4.3.3
    Copyright (C) 2008 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    51
    I am using Xcode

    I uncommented the destructor declearation in the class Nation
    Code:
    ~Nation();
    and added a destructor definition
    Code:
    //a default constructor
    Nation::Nation(){}
    Nation::~Nation(){}
    compiled and ran it but got the same error after the first name input.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    If it cannot reach the message "What is your name player 2?" then there is most likely something you left out when posting this, as neither takeTuen, nor menu will be getting called and what's left we can determine to be fine simply by examination.
    As confirmation, we know that you at least left out the destructor definition.
    Thus the bug is probably in the code you left out. Or perhaps this is not successfully compiling when you make a change and is just re-running a previous build that had errors.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structures and dynamically allocated arrays
    By Bandizzle in forum C Programming
    Replies: 7
    Last Post: 10-04-2009, 02:05 PM
  2. Dynamically allocated array
    By dre in forum C Programming
    Replies: 17
    Last Post: 08-13-2009, 06:57 PM
  3. Will free() clean up this allocated memory?
    By simguy in forum C Programming
    Replies: 3
    Last Post: 01-18-2007, 04:21 PM
  4. Where are pointers allocated in memory?
    By sparks in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2005, 12:52 PM
  5. Test pointer if successfully freed.
    By UniMord in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 09:09 AM

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