After running the following .cpp file within Visual Studios, I receive the following error: Exception thrown at 0x0074425E in HomeWork3.exe: 0xC0000005: Access violation reading location 0x00000000.
The error comes from the following slew of code:
int Table::getData(int r, int c) const {
return columns[r]->getData(c);
};
The entire code is as follows:
//Table.cpp
#include <cstdlib>
#include "Table.h"
Table::Table(unsigned int r, unsigned int c) {
szRow = r;
szCol = c;
columns = new RowAray*[r];
for (int i = 0; i < r; i++) {
columns[i] = new RowAray(c);
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
columns[i]->setData(j, (rand() % 90 + 10));
}
}
}
Table::~Table() {
for (int i = 0; i < szRow; i++) {
columns[i] = 0;
delete[] columns[i];
}
columns = 0;
delete[] columns;
}
Table::Table(const Table &t) {
szRow = t.szRow;
szCol = t.szCol;
columns = t.columns;
};
int Table::getData(int r, int c) const {
return columns[r]->getData(c);
};
void Table::setData(int r, int c, int value) {
columns[r]->setData(c, value);
}
//main.cpp
//User Libraries
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <iomanip>
using namespace std;
//User Libraries
#include "PlusTab.h"
//Global Constants
//Function Prototype
void prntTab(const Table &);
//Execution Begins Here!
int main(int argc, char** argv) {
//Initialize the random seed
srand(static_cast<unsigned int>(time(0)));
//Declare Variables
int rows = 3, cols = 4;
//Test out the Tables
PlusTab tab1(rows, cols);
PlusTab tab2(tab1);
PlusTab tab3 = tab1 + tab2;
//Print the tables
cout << "Abstracted and Polymorphic Print Table 1 size is [row,col] = ["
<< rows << "," << cols << "]";
prntTab(tab1);
cout << "Copy Constructed Table 2 size is [row,col] = ["
<< rows << "," << cols << "]";
prntTab(tab2);
cout << "Operator Overloaded Table 3 size is [row,col] = ["
<< rows << "," << cols << "]";
prntTab(tab3);
//Exit Stage Right
system("pause");
return 0;
}
void prntTab(const Table &a) {
cout << endl;
for (int row = 0; row<a.getSzRow(); row++) {
for (int col = 0; col<a.getSzCol(); col++) {
cout << setw(4) << a.getData(row, col);
}
cout << endl;
}
cout << endl;
}
//RowAray.cpp
#include <cstdlib>
#include "RowAray.h"
RowAray::RowAray(unsigned int n)
{
this->size = n;
rowData = new int[n];
}
RowAray::~RowAray()
{
delete[]rowData;
}
void RowAray::setData(int i, int r)
{
this->rowData[i] = r;
}
//Table.h
#ifndef TABLE_H
#define TABLE_H
#include "AbsTabl.h"
class Table :public AbsTabl {
public:
Table(unsigned int, unsigned int);
Table(const Table &);
virtual ~Table();
int getSzRow()const { return szRow; }
int getSzCol()const { return szCol; }
int getData(int, int)const;
void setData(int, int, int);
};
#endif /* TABLE_H */
//AbsRow.h
#ifndef ABSROW_H
#define ABSROW_H
class AbsRow {
protected:
int size;
int *rowData;
public:
virtual int getSize()const = 0;
virtual int getData(int)const = 0;
};
#endif /* ABSROW_H */
//AbsTabl.h
#ifndef ABSTABL_H
#define ABSTABL_H
#include "RowAray.h"
class AbsTabl {
protected:
int szRow;
int szCol;
RowAray **columns;
public:
virtual int getSzRow()const = 0;
virtual int getSzCol()const = 0;
virtual int getData(int, int)const = 0;
};
#endif /* ABSTABL_H */
//PlusTabl.h
#ifndef PLUSTAB_H
#define PLUSTAB_H
#include "Table.h"
class PlusTab :public Table {
public:
PlusTab(unsigned int r, unsigned int c) :Table(r, c) {};
PlusTab operator+(const PlusTab &t);
};
#endif /* PLUSTAB_H */
//RowAray.h
#ifndef ROWARAY_H
#define ROWARAY_H
#include "AbsRow.h"
class RowAray :public AbsRow {
public:
RowAray(unsigned int);
virtual ~RowAray();
int getSize()const { return size; }
int getData(int i)const {
if (i >= 0 && i<size)return rowData[i];
else return 0;
}
void setData(int, int);
};
#endif /* ROWARAY_H */
//PlusTab.cpp
#include "PlusTab.h"
PlusTab PlusTab::operator+(const PlusTab &t) {
PlusTab tab(this->getSzRow(), this->getSzCol());
for (int i = 0; i < tab.getSzRow(); i++) {
for (int j = 0; j <tab.getSzCol(); j++) {
(tab.columns[i])->setData(j, this->getData(i, j) + t.getData(i, j));
}
}
return tab;
}
I am not sure why this is happening.
Access Reading Violation means that a function is receiving NULL
(trying to read from an unauthorized location of memory). Hence, whatever you're pointing at isn't a valid pointer. In your example, I think this is coming from columns
in getData
. Make sure getData
isn't trying to access a NULL
pointer , or do a quick check (assert):
if (columns == NULL)
return NULL;
return columns[r]->getData(c);
User contributions licensed under CC BY-SA 3.0