Disappearing Data

I have a very strange situation here this code I wrote here will not run in visual studio when debugging it when it calls a function sometimes the get_size_of_ants_vector and sometimes it is another function the data passed to it is fine and then the function is called and the data just disappears and all the memory locations are empty just random data in the variables .My teacher spent hours today trying to figure this out. A friend of mine uses CodeBlocks he ran my code and with the exception of removing the header files that are only used in visual studio the "stdafx.h" files the same code runs fine.I have to know why this is happening and how to fix it.There is alot of code to save space i will post the class that is causing the problem and the main file. Please let me know if you need more. So if anyone has run into a similar situation with other code or knows why this happens please let me know. There has to be a reason why this is happening I would very much appreciate it thank you.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#ifndef GAMEBOARD_H
#define GAMEBOARD_H

//#pragma once

#include <iostream>
#include "Location.h"
#include "Ant.h"
#include "DoodleBug.h"
#include <vector>

class GameBoard
{
public:
	GameBoard( ); 
	Location check_for_empty_cell(Location& current_cell); 
	Location pick_random_surrounding_cell(Location& current_cell); 
	char get_contents_of_chosen_cell(Location chosen_cell); //removed refernce
	void remove_organism_from_gameBoard(Location& cell); 
	void add_ant_to_gameBoard(Ant& ant); 
	void add_doodleBug_to_gameBoard(DoodleBug& player); 
	Ant& get_ant_from_ants_vector(int index);
	DoodleBug& get_doodleBug_from_doodlebug_vector(int index);
	void move_ant(Ant& ant_2_move);
	void breed_ant(Ant& ant_2_breed);
	int get_size_of_ants_vector( );	
	int get_size_of_doodleBugs_vector( );
	void populate_gameBoard( );
	void draw_gameBoard_2_console( );

private:
	Organism m_gameBoard[20][20];
	std::vector<Ant> m_ants;
	std::vector<DoodleBug> m_doodleBugs;
};
#endif //GAMEBOARD_H

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165

#include "stdafx.h"
#include "GameBoard.h"


GameBoard::GameBoard( )   // populates gameboard with empty organisms
{
	for(int i =0; i < 20; i++)
	{
		for(int j =0; j < 20; j++)
			m_gameBoard[i][j] = Organism(Location(i, j), '*'); 
	}
	populate_gameBoard( ); // places ants and doodlebugs in vectors and on gameboard
}

void GameBoard::remove_organism_from_gameBoard(Location& cell)
{
	m_gameBoard[cell.get_xpoint()][cell.get_ypoint()] = Organism(cell, '*');
}

void GameBoard::add_ant_to_gameBoard(Ant& ant)
{
	m_gameBoard[ant.get_location().get_xpoint()][ant.get_location().get_ypoint()] = ant;
}

void GameBoard::add_doodleBug_to_gameBoard(DoodleBug& doodleBug)
{
	m_gameBoard[doodleBug.get_location().get_xpoint()][doodleBug.get_location().get_ypoint()] = doodleBug;
}

Location GameBoard::pick_random_surrounding_cell(Location& current_cell)
{
	switch(rand( ) % 4)
	{
	case 0: //<--- UP
		if(current_cell.get_xpoint( ) > 0)
			return Location((current_cell.get_xpoint()-1), current_cell.get_ypoint());

	case 1: //<--- DOWN
		if (current_cell.get_xpoint( ) < 19)
			return Location((current_cell.get_xpoint()+1), current_cell.get_ypoint());

	case 2: //<--- LEFT
		if (current_cell.get_ypoint( ) > 0)
			return Location((current_cell.get_xpoint()), current_cell.get_ypoint()-1);

	case 3: //<--- RIGHT
		if(current_cell.get_xpoint( ) < 19)
			return Location((current_cell.get_xpoint()), current_cell.get_ypoint()+1);
	}
	return current_cell;
}

char GameBoard::get_contents_of_chosen_cell(Location chosen_cell) //removed reference
{
	return m_gameBoard[chosen_cell.get_xpoint( )][chosen_cell.get_ypoint( )].get_species( );
}

Location GameBoard::check_for_empty_cell(Location& current_cell) 
{
	if(current_cell.get_xpoint() > 0) //
		if(m_gameBoard[current_cell.get_xpoint()-1][current_cell.get_ypoint()].get_species() == '*')
			return Location((current_cell.get_xpoint()-1), current_cell.get_ypoint());

	if(current_cell.get_xpoint() < 19) //<--- Down
		if(m_gameBoard[current_cell.get_xpoint()+1][current_cell.get_ypoint()].get_species() == '*')
			return Location((current_cell.get_xpoint()+1), current_cell.get_ypoint());
	
	if(current_cell.get_ypoint() > 0) //<--- Left
		if(m_gameBoard[current_cell.get_xpoint()][current_cell.get_ypoint()-1].get_species() == '*')
			return Location((current_cell.get_xpoint()), current_cell.get_ypoint()-1);

	if(current_cell.get_ypoint() < 19) //<--- Right
		if(m_gameBoard[current_cell.get_xpoint()][current_cell.get_ypoint()+1].get_species() == '*')
			return Location((current_cell.get_xpoint()), current_cell.get_ypoint()+1);
	
	return current_cell; // All are Occupied
}

void GameBoard::populate_gameBoard( )
{
	while(m_ants.size() < 100)
	{
		int x = rand( ) % 20;
		int y = rand( ) % 20;

		if (get_contents_of_chosen_cell(Location(x, y)) == '*')
		{
			m_ants.push_back(Ant(Location(x, y), 'A', 0));
			m_gameBoard[x][y] = (Ant(Location(x, y), 'A', 0));
		}
	}
	while (m_doodleBugs.size() < 5)
	{
		int x = rand( ) % 20;
		int y = rand( ) % 20;

		if (get_contents_of_chosen_cell(Location(x, y)) == '*')
		{
			m_doodleBugs.push_back(DoodleBug(Location(x, y), 'D', 0, 0));
			m_gameBoard[x][y] = (DoodleBug(Location(x, y), 'D', 0, 0));
		}
	}
}

void GameBoard::move_ant(Ant& ant_2_move)
{
	Location desired_cell = pick_random_surrounding_cell(ant_2_move.get_location());
	Location current_cell = ant_2_move.get_location();

	if (get_contents_of_chosen_cell(desired_cell) == '*')
	{
		remove_organism_from_gameBoard(ant_2_move.get_location());  
		ant_2_move.set_location(desired_cell);
		add_ant_to_gameBoard(ant_2_move);
		
		(ant_2_move.get_steps_till_breed() == 3) ? breed_ant(ant_2_move) : 
			ant_2_move.set_steps_till_breed(ant_2_move.get_steps_till_breed()+1);
	}
} 

void GameBoard::breed_ant(Ant& ant_2_breed)
{
	Location spawns_cell = check_for_empty_cell(ant_2_breed.get_location());

	if (spawns_cell != ant_2_breed.get_location())
	{
		m_ants.push_back(Ant(spawns_cell, 'A', 0));
		m_gameBoard[spawns_cell.get_xpoint()][spawns_cell.get_ypoint()] 
		= Ant(spawns_cell, 'A', 0);
		ant_2_breed.set_steps_till_breed(0);
	}
}

void GameBoard::draw_gameBoard_2_console( )
{
	for(int i =0; i < 20; i++)
	{
		for(int j =0; j < 20; j++)
		{
			std::cout << get_contents_of_chosen_cell(Location(i, j)) << " ";	
		}
		std::cout << std::endl;
	}
}

int GameBoard::get_size_of_ants_vector( )
{
	return m_ants.size();
}

int GameBoard::get_size_of_doodleBugs_vector( )
{
	return m_doodleBugs.size();
}

Ant& GameBoard::get_ant_from_ants_vector(int index)
{
	return m_ants[index];
}

DoodleBug& GameBoard::get_doodleBug_from_doodlebug_vector(int index)
{
	return m_doodleBugs[index];
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "stdafx.h"
#include "Ant.h"
#include "DoodleBug.h"
#include "GameBoard.h"
#include "Location.h"
#include <Windows.h>

int _tmain(int argc, _TCHAR* argv[])
{
	GameBoard test_gameBoard;
	

	test_gameBoard.draw_gameBoard_2_console( );
	std::cout << std::endl;
	
	while (true)
	{
		for (int i =0; i < test_gameBoard.get_size_of_ants_vector(); i++) 
		{
			test_gameBoard.move_ant(test_gameBoard.get_ant_from_ants_vector(i));
		}

		std::cout << "Ants: "<< test_gameBoard.get_size_of_ants_vector() << std::endl;
		std::cout << std::endl;
		test_gameBoard.draw_gameBoard_2_console( );
		Sleep(1000);
	}


	system("PAUSE");
	return 0;
}
Last edited on
this code is really missed up, my best guess is that you forgot the include guards.
How is it messed up? How do I solve the problem? Do I need to post more code? Please don't just just tell me it is messed up. the guards are included the first line of code is the start of a guard.
Last edited on
I don't see what Rechard3 was referring to as messed up.

@Rechard3 - Please be specific when telling someone their code is messed up.

Your include guards look fine for gameboard.h.

Posting gameboard.h, gameboard.cpp, main.cpp in separate
[code]
[/code]
blocks might have made it clearer those were separate files, but that is very minor.

You've not posted enough code for anyone to try and replicate your problem. Too many missing header files.

Please try any reduce your problem to the minimum amount of code to replicate the problem, then post that.

I would suggest staying away from variable names that being with _. The standard reserves those for the implementation specific use. You might possibly have a conflict there. A common convention is to use m_ as the prefix for member variables.

Another small point (unrelated to your problem). It's not necessary to track number_of_ants and number_of_doodle_bugs yourself. Your vectors track this information. get_number_of_ants should simply return doodleboog.size(). Actually, get_size_of_ants_vector and get_number_of_ants do the same thing, assuming all the ants are maintained in the vector.





Thanks, I am getting a "DoodleBug Game v@.exe has triggered a break-point". and it points to the "retval = HeapFree(_crtheap, 0, pBlock); line in the free.c file. Every time I Google this error it has to do with pointers but I am not using any pointers. I have narrowed it down to the breed line in the move_ant function if I comment that out it runs fine. I will make those changes you suggested, clean up my post and see what happens thank you very much.
Here is the Ant header file and Organism header that it inherits from, and the Location. Please let me know if you anymore thanks again.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef ANT_H
#define ANT_H

#pragma once

#include "Organism.h"

class Ant : public Organism 
{
public:
	Ant( );
	Ant(Location postion, char species, int steps);
	char get_species( ); 
	Location& get_location( );
	void set_steps_till_breed(int count);
	int get_steps_till_breed( );

private:
	int m_steps_till_breed;
};
#endif //ANT_H 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#ifndef ORGANSIM_H
#define ORGANSIM_H

#pragma once

#include "location.h"
#include <iostream>


class Organism	
{
public:
	Organism( );
	Organism(Location postion, char species);
	Organism(const Organism& other);
	Organism& operator=(const Organism& other);
	Location& get_location( );
	void set_location(Location new_location);
	virtual char get_species( ) { return m_species; } 

private:	
	Location m_points;
	char m_species;
};

#endif // ORGANSIM_H 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#ifndef LOCATION_H
#define LOCATION_H

#pragma once

#include <iostream>

class Location	
{
public:
	Location( );
	Location(int x, int y);
	Location(const Location& other);
	Location& operator=(const Location& other);
	friend bool operator==(const Location& lhs, const Location& rhs);
	friend bool operator!=(const Location& lhs, const Location& rhs);
	int get_xpoint( ) const;
	int get_ypoint( ) const;
	void set_xpoint(const int x); 
	void set_ypoint(const int y);

private:
	int m_x;
	int m_y;
};
#endif //LOCATION_H 
I am getting a "DoodleBug Game v@.exe has triggered a break-point". and it points to the "retval = HeapFree(_crtheap, 0, pBlock); line in the free.c file. Every time I Google this error it has to do with pointers but I am not using any pointers.

That error appears to do with freeing dynamic memory from the heap. Even though you not explicitly using dynamic memory, containers such as vector do use dynamic memory internally.

There are several common run-time errors when freeing memory from the heap.
1) Trying to release memory that is not allocated
2) Trying to release the same memory more than once (really the same as #1).
3) The heap has become corrupted.
Since it appears the only use of dynamic memory in your program is by vector, then the likely cause is #3.

I would suggest you look closely at your indexing of m_ants and m_doodlebugs. My guess is that you're overwriting past the end of the vector threby corrupting the internal pointers of the heap.
Thanks for your help I really appreciate it but I don't think this has anything to do with vectors I just rewrote the whole thing with arrays instead of vectors.
Same thing. I pasted the entire code on paste-bin if anyone has time to run
it I don't know what else to do i have rewrote this three times now.I put markers in the code in the main. if you stop the debugger when the "Outer loop count" in on 7 and the doodlebug for loop i is on 7. step through it and all the data just disappears out of no were. put your breakpoint on the main function line 608 and stop the debugger when "Outer loop count is 7. Thanks

http://pastebin.com/6gNLXQDS
You failed to paste the code for your Location type.

After correcting your code so it actually compiles without VS specific compiler extensions and correcting the const correct issues the code had as a result of the previous fix, I used the following Location fill-in:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class Location
{
public:
    int get_xpoint() const { return _x ; }
    int get_ypoint() const { return _y ; }

    void set_xpoint(int x) { _x = x ; validate() ; }
    void set_ypoint(int y) { _y = y ; validate() ; }

    Location( int x=0, int y=0 ) : _x(x), _y(y) 
        { validate() ; }


private:

    void validate() const
    {
        std::ostringstream os ;
        os << "Location ERROR: " ;
        bool invalid = false ;

        if  ( _x < 0 || _x > 19 )
        {
            os << "x = " << _x ;
            invalid = true ;
        }

        if ( _y < 0 || _y > 19)
        {
            if ( invalid )
                os << ", " ;
            os << "y = " << _y ;
            invalid = true ;
        }

        if ( invalid )
            throw std::runtime_error(os.str()) ;
    }

    int _x, _y ;
};

inline bool operator==( const Location& a, const Location& b )
{
    return a.get_xpoint() == b.get_xpoint() && a.get_ypoint() == b.get_ypoint() ;
}

inline bool operator!=( const Location& a, const Location & b )
{
    return !(a==b) ;
}


I right-clicked on line 37 above and chose "Run to cursor". I then checked the stack trace to see where the erroneous Location was being created. It was being created on line 19 of the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
       Location GameBoard::pick_random_surrounding_cell(const Location& current_cell)
        {
                switch(rand( ) % 4)
                {
                case 0: //<--- UP
                        if(current_cell.get_xpoint( ) -1 >= GAMEBOARD_EDGE_BOTTOM_SIDE )
                                return Location((current_cell.get_xpoint()-1), current_cell.get_ypoint());
 
                case 1: //<--- DOWN
                        if (current_cell.get_xpoint( ) +1 <= GAMEBOARD_EDGE_HIGH_SIDE)
                                return Location((current_cell.get_xpoint( )+1), current_cell.get_ypoint( ));
 
                case 2: //<--- LEFT
                        if (current_cell.get_ypoint( ) -1 >= GAMEBOARD_EDGE_BOTTOM_SIDE)
                                return Location((current_cell.get_xpoint( )), current_cell.get_ypoint( )-1);
 
                case 3: //<--- RIGHT
                        if(current_cell.get_xpoint( ) +1 <= GAMEBOARD_EDGE_HIGH_SIDE)
                                return Location((current_cell.get_xpoint( )), current_cell.get_ypoint( )+1);
                }
                return current_cell;
        }


See anything odd there?

I also noticed that the number of ants in your simulation is negative after iteration 28 or so of the "Outer loop" and keeps decreasing with each subsequent iteration. Should that be possible?
Last edited on
Thanks for your help I really appreciate it but I don't think this has anything to do with vectors I just rewrote the whole thing with arrays instead of vectors.

So? Writing past the end of a C-style array will corrupt your heap just as surely as writing past the end of a vector.
Cire, you solved my problem. I can't thank you enough.
Last edited on
Topic archived. No new replies allowed.