Allocated memory release before end of program

时间:2016-02-03 02:44:13

标签: c++ memory

The program I'm writing is a Sudoku puzzle. Each puzzle is a 2D array of struct, each struct is an int and a bool. The bool represents if the number in each element of the array is a clue or not, clues cannot be changed. Sorry for huge blocks of code. I pared it down as much as possible for easy readability.

There is a missing function ShowBoardAns from Sudoku.h. It does the same as ShowBoard, but in different colors.

After the first time my program displays the puzzle (using ShowBoard from Sudoku.h), it appears that both Puzzle and Comp are deleted from memory. After entering a number for an answer (say 1 for x, 1 for y and 1 for answer), when the board is printed again, it either holds garbage or does not print anything except the outline.

When debugging using Visual Studio, Puzzle and Comp start to hold garbage rather than the numbers the program assigned. As far as I can tell, this happens after the program goes back to Main from Sudoku after displaying the puzzle the first time.

#ifndef Sudoku_h
#define Sudoku_h

#include <iostream>
#include <Windows.h>    // Header file needed to change the text color of                       console
using namespace std;

class Sudoku
{
protected:
struct Puzz {
    int Ans;
    bool IsClue;
};
public:

Sudoku() {};        // Default constructor
Sudoku(Puzz [][9]); // Constructor
 ~Sudoku()  {}; // Destructor
bool IsCorrect(int [][9], Puzz [][9]);      // Checks the puzzle to determine accuracy
void ShowBoard(Puzz [][9]) const;
void ShowBoardAns(Puzz [][9]) const;
};


/* Constructor
Each puzzle array is initalized here with the complete numbers.
*/
Sudoku::Sudoku(Puzz Arr[9][9])
{
ShowBoard(Arr);
}

bool Sudoku::IsCorrect(int Comp[][9], Puzz Arr[9][9])
{
bool Correct = false;   // Init to false
for (int x = 0; x < 9; x++)
{
    for (int y = 0; y < 9; y++)
    {
        if (Comp[x][y] != Arr[x][y].Ans)                    
            return Correct = false;
        else
            Correct = false;
    }
}

return Correct; }

/*
ShowBoard displays the incomplete puzzle to the user the first time that
they see it. After they have started entering answers, ShowBoardAns is called         
to show the user different text colors.
*/
void Sudoku::ShowBoard(Puzz Arr[][9]) const
{
HWND console = GetConsoleWindow();
RECT r;
GetWindowRect(console, &r);
MoveWindow(console, r.left, r.top, 800, 450, TRUE);
HANDLE hConsole;
int Color = 7;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE); 
SetConsoleTextAttribute(hConsole, Color);
int x, y;
cout << "  ";
for (int c = 1 ; c <= 9; c++)
{
    cout << "   " << c;
}
cout << "\n   _____________________________________\n" << endl;
    for (x = 0; x < 9; x++)
    {
        cout << x+1 << "  | ";
        for (y = 0; y < 9; y++)
        {
            // To assign the colors, if the element is a clue, it is green,
            // otherwise, 0s are white.
            if (Arr[x][y].IsClue == true)
            {
                Color = 10;
                SetConsoleTextAttribute(hConsole, Color);
                cout << Arr[x][y].Ans;
                Color = 7;
                SetConsoleTextAttribute(hConsole, Color);
                cout << " | ";
            }
            else 
            {
                Color = 15;
                SetConsoleTextAttribute(hConsole, Color);
                cout << Arr[x][y].Ans;
                Color = 7;
                SetConsoleTextAttribute(hConsole, Color);
                cout << " | ";
            }
        }
        cout << "\n   _____________________________________\n" << endl;
    }
}


#endif

#include <iostream>
#include "Sudoku.h"
using namespace std;

class Easy : protected Sudoku
{
private:
int Comp[9][9];     // Comp is the completed puzzle
Puzz Puzzle[9][9];  // Puzzle is what the user sees
Sudoku EasyPuzzle;

public:
Easy();
Easy(int);
~Easy()
{ delete [] &Puzzle; }
void AddNum(int, int, int); // Add a number to solve the puzzle
bool Solve();
void ShowPuzz();
};

Easy::Easy()
{
for (int x = 0; x < 9; x++)
{
    for (int y = 0; y < 9; y++)
    {
        Comp[x][y] = 0;
        Puzzle[x][y].Ans = 0;
        Puzzle[x][y].IsClue = false;
    }
}
}

/*
The default constructor initializes the arrays to hold the
completed and partial puzzles.
*/
Easy::Easy(int A)
{
int Comp[][9] = { {3, 7, 6, 1, 4, 2, 9, 5, 8},
{5, 8, 4, 6, 7, 9, 2, 1, 3},
{9, 1, 2, 8, 3, 5, 4, 7, 6},
{6, 9, 1, 2, 5, 4, 3, 8, 7},
{8, 4, 5, 3, 6, 7, 1, 9, 2},
{2, 3, 7, 9, 1, 8, 5, 6, 4},
{4, 6, 9, 5, 8, 3, 7, 2, 1},
{7, 2, 8, 4, 9, 1, 6, 3, 5},
{1, 5, 3, 7, 2, 6, 8, 4, 9} };


Puzz Puzzle[][9] = { {{0, false}, {0, false}, {0, false}, {0, false}, {4,   true},  {0, false}, {9, true},  {0, false}, {0, false}},
{{0,  false},  {8,  true},   {0,  false},  {6,  true},   {7,  true},   {0,      false},  {0,  false},  {0,  false},  {0,  false}},
{{9,  true},   {0,  false},  {2,  true},   {8,  true},   {0,  false},  {0,  false},  {4,  true},   {0,  false},  {0,  false}},
{{0,  false},  {9,  true},   {1,  true},   {0,  false},  {0,  false},  {0,  false},  {0,  false},  {0,  false},  {0,  false}},
{{0,  false},  {4,  true},   {0,  false},  {3,  true},   {6,  true},   {0,  false},  {0,  false},  {0,  false},  {2,  true }},
{{0,  false},  {0,  false},  {0,  false},  {0,  false},  {0,  false},  {0,  false},  {5,  true},   {0,  false},  {4,  true }},
{{0,  false},  {0,  false},  {0,  false},  {0,  false},  {0,  false},  {0,  false},  {7,  true},   {0,  false},  {1,  true }},
{{0,  false},  {2,  true},   {8,  true},   {0,  false},  {0,  false},  {1,  true},   {0,  false},  {3,  true},   {0,  false}},
{{1,  true},   {0,  false},  {3,  true},   {7,  true},   {0,  false},  {6,  true},   {8,  true},   {0,  false},  {0,  false}} };

Sudoku EasyPuzzle(Puzzle);
}

/*
AddNum lets a user select the X and Y coordinates of where
they would like to enter a new number and, after checking to 
make sure it is not one of the hint numbers, will update the 
Puzzle array
*/
void Easy::AddNum(int X, int Y, int Answer)
{
// Subtract one from both X and Y to look at the correct subscript.
X-= 1;
Y-= 1;
if (Puzzle[X][Y].IsClue == true) 
{
        cout << "Cannot change a puzzle clue. Please try again." << endl;
        return;
}
else
{
    Puzzle[X][Y].Ans = Answer;
    EasyPuzzle.ShowBoardAns(Puzzle);
}}


#include <iostream>
#include "Sudoku.h"
#include "Easy.h"
#include <Windows.h>

using namespace std;

int Menu(); // Menu function prototype
void EasyPuzz(Easy);

// Constants for the menu choices
const int EASY = 1,
      MED = 2,
      HARD = 3,
      QUIT = 4;

int main()
{
int Choice;


cout << "\t\t\tWelcome to Sudoku!\n\n" << endl;


Choice = Menu();    // Calls the menu function
if (Choice == 1)
{
    cout << "You have chosen an easy puzzle. When doing the puzzle, you "
         << "will enter an X and\nY coordinate to change an answer. If you "
         << "enter a coordinate that is a clue, you will receive an error. " 
         << "If you'd like to check to see if you've solved the puzzle "
         << "type S instead of coordinates.\n" << endl;
    Easy EPuzzle(1);
    //EPuzzle.ShowPuzz();
    EasyPuzz(EPuzzle);
}
else if (Choice == 2)
{
    cout << "You have chosen a medium puzzle. When doing the puzzle, you "
         << "will enter an X and\nY coordinate to change an answer. If you "
         << "enter a coordinate that is a clue, you will receive an error."
         << "If you'd like to check to see if you've solved the puzzle "
         << "type S instead of coordinates.\n"<< endl;
    Medium MPuzzle;
    MedPuzz(MPuzzle);
}
else if (Choice == 3)
{
    cout << "You have chosen a hard puzzle. When doing the puzzle, you "
         << "will enter an X and\nY coordinate to change an answer. If you "
         << "enter a coordinate that is a clue, you will receive an error. "
         << "You can get a hint by entering H instead of a number."
         << "If you'd like to check to see if you've solved the puzzle "
         << "type S instead of coordinates.\n"<< endl;
    Hard HPuzzle;
    HardPuzz(HPuzzle);
}
else if (Choice == 4)
{
    cout << "Thank you for playing!" << endl;
    exit(0);
}

0 个答案:

没有答案