I am receiving an unhandled exception when I try to set constructor array to zero. I think the issue is with the constructor, but I am not sure. I chopped out the unnecessary code and reproduced the error. Thanks for the feedback.
The error says: Unhandled exception at 0x0fa4025: Access violation writing location 0x0000.
source:
#include <iostream>
#include <vector>
#include "header.h"
using namespace std;
foo::foo(int b[3][3])
{
memset(b, 0, sizeof b);
}
void foo::makeMove()
{
board[1][1] = 1;
}
void foo::print()
{
for (int i = 0; i < 2; i++){
for (int j = 0; j < 2; j++){
cout << board[i][j];
}
}
}
header:
class foo
{
public:
foo::foo(int[3][3] = 0);
void makeMove();
void print();
private:
int board[3][3];
};
main:
#include "header.h" // include definition of class TicTacToe
int main()
{
foo g; // creates object g of class TicTacToe
g.makeMove(); // invokes function makeMove
g.print();
} //
end main
foo::foo(int[3][3] = 0);
The default value for the argument, which is a pointer, not an array, has been set to 0
. It is invalid when no arguments are passed to the constructor. As such, this is not a reasonable default.
You then write to it via memset
, which invokes undefined behavior. I don't even see the point of this design. Why are you taking an external array and setting it to all 0's? Your class has its own array board
. Use that.
Next, realize that this is C++ and you are writing your code as if it were C. You have vectors, std::copy
, etc.
This happens because to specified default value of the pointer (that points to the array, of course): foo::foo(int[3][3] = 0);
. Hence, you are trying to memset
0x0.
Your constructor writes to b
without checking if it's 0. You default b
to 0, so you invoke undefined behavior.
Add a null check before the memset
in the CTOR.
You created an object of class foo
foo g;
using the default constructor with default argument equal to 0.
foo::foo(int[3][3] = 0);
When you tries to access the memory with adrress 0 to fill it
foo::foo(int b[3][3])
{
memset(b, 0, sizeof b);
}
Also in my opinion this constructor
foo::foo(int b[3][3])
{
memset(b, 0, sizeof b);
}
has no any sense because it deals with local variable b. I think you wanted to fill data member int board[3][3]; not the local variable.
I think your main mistake is to define a constructor that became the 'default' constructor. This is because you gave it a default value, and so can call it with no arguments. Your loops are also wrong. See below.
#include <iostream>
#include <vector>
#include <memory.h>
using namespace std;
class foo
{
public:
void makeMove();
void print();
private:
int board[3][3] { { 0 }, { 0 }, { 0 } };
};
void foo::makeMove()
{
board[1][1] = 1;
}
void foo::print()
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cout << board[i][j];
}
cout << endl;
}
}
int main()
{
foo g; // creates object g of class TicTacToe
g.makeMove(); // invokes function makeMove
g.print();
} //
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.