C++ 动态分配的二维数组访问问题C++;
我在访问2d数组元素时遇到问题。 创建对象时,我在构造函数中为数组分配内存 当我尝试访问数组元素时,一旦创建了对象,我就获得了EXC_BAD_访问权 我检查过了,当我还在构造函数中时,我可以访问数组元素。 我不知道我做错了什么 这是我的类,我为数据数组分配内存C++ 动态分配的二维数组访问问题C++;,c++,arrays,C++,Arrays,我在访问2d数组元素时遇到问题。 创建对象时,我在构造函数中为数组分配内存 当我尝试访问数组元素时,一旦创建了对象,我就获得了EXC_BAD_访问权 我检查过了,当我还在构造函数中时,我可以访问数组元素。 我不知道我做错了什么 这是我的类,我为数据数组分配内存 class TableData { public: TableData(std::string name, int rows, int columns) : tableName(name), rowCount(rows), col
class TableData
{
public:
TableData(std::string name, int rows, int columns) : tableName(name), rowCount(rows), columnCount(columns)
{
data = new std::string*[rowCount];
for (int count = 0; count < rowCount; ++count)
data[count] = new std::string[columnCount];
columnID.resize(columnCount);
//Below I did a test where can see that I can access array elements
data[0][0]=“test1111”;
std::string test = data[0][0];
}
~TableData()
{
for (int count = 0; count < rowCount; ++count)
delete[] data[count];
delete[] data;
}
std::string **data;
std::string tableName = "";
const int rowCount;
const int columnCount;
std::vector<std::string> columnID;
};
“tables”是TableData对象的向量
以下是完整的功能:
void Decoder::getTables(std::fstream &fs, std::vector<char> & buffer, SGBDdata &ecuData)
{
const int ptr_offset = 0x84;
int tableOffset = *reinterpret_cast<int32_t*>(&buffer[0] + ptr_offset);
const int tableCountBufferLength = 4;
vector<char> tableCountBuffer;
fs.seekg(tableOffset, fs.beg);
readAndDecryptBytes(fs, tableCountBuffer, tableCountBufferLength);
int tableCount = *reinterpret_cast<int32_t*>(&tableCountBuffer[0] + 0);
for(int i = 0; i < tableCount; ++i) //iterate through tables
{
int tableBufferLength = 0x50;
vector<char> tableBuffer;
readAndDecryptBytes(fs, tableBuffer, tableBufferLength);
string name(tableBuffer.begin(), tableBuffer.begin() + 0x40);
TrimEnd(name);
int tableColumnOffset = *reinterpret_cast<int32_t*>(&tableBuffer[0] + 0x40);
int tableColumnCount = *reinterpret_cast<int32_t*>(&tableBuffer[0] + 0x48);
int tableRowCount = *reinterpret_cast<int32_t*>(&tableBuffer[0] + 0x4C);
//**This is where I am creating new “TableData” object and putting it into vector<TableData>**
ecuData.tables.push_back(TableData(name, tableRowCount, tableColumnCount));
long savedPos = fs.tellg();
fs.seekg(tableColumnOffset, fs.beg);
//Load column names
for(int j = 0; j < tableColumnCount; ++j)
{
int tableItemBufferLength = 1024;
vector<char> tableItemBuffer;
for(int k = 0; k < tableItemBufferLength; ++k)
{
readAndDecryptBytes(fs, tableItemBuffer, 1);
if (tableItemBuffer[k] == 0)
break;
}
ecuData.tables[i].columnID.push_back(string(tableItemBuffer.begin(), tableItemBuffer.end() - 1));
}
for(int j = 0; j < tableRowCount; ++j)
{
for (int k = 0; k < tableColumnCount; ++k)
{
int tableItemBufferLength = 1024;
vector<char> tableItemBuffer;
for (int l = 0; l < tableItemBufferLength; ++l)
{
readAndDecryptBytes(fs, tableItemBuffer, 1);
if (tableItemBuffer[l] == 0)
break;
}
string s(tableItemBuffer.begin(), tableItemBuffer.end() - 1);
ecuData.tables[i].data[j][k] = s; //**This is where I get my fault**
}
}
fs.seekg(savedPos, fs.beg);
}
}
void解码器::getTables(std::fstream&fs,std::vector&buffer,SGBDdata&ecuData)
{
常数int ptr_偏移量=0x84;
int tableOffset=*重新解释强制转换(&buffer[0]+ptr\u offset);
const int tableCountBufferLength=4;
向量表缓冲区;
fs.seekg(表偏移量,fs.beg);
读取和解密字节(fs、tableCountBuffer、tableCountBufferLength);
int tableCount=*重新解释强制转换(&tableCountBuffer[0]+0);
for(int i=0;i
这里是SGBDdata类。此类的ecuData对象包含向量数据
class SGBDdata
{
public:
std::string sgbdPath;
std::string ecuName;
std::vector<JobData> jobs;
std::vector<TableData> tables;
};
SGBDdata类
{
公众:
std::字符串sgbdPath;
std::字符串名称;
向量作业;
std::向量表;
};
丢失所有新建、删除和指针魔法。使用向量的向量
// Example program
#include <iostream>
#include <string>
#include <vector>
class TableData
{
public:
TableData(std::string name, int rows, int columns) : tableName(name), rowCount(rows), columnCount(columns)
{
data.resize(columnCount);
for( int i = 0; i < columnCount; i++)
{
data[i].resize(rowCount);
}
columnID.resize(columnCount);
//Below I did a test where can see that I can access array elements
data[0][0]="test1111";
std::string test = data[0][0];
}
~TableData()
{
}
std::vector<std::vector<std::string>> data;
std::string tableName = "";
const int rowCount;
const int columnCount;
std::vector<std::string> columnID;
};
int main()
{
TableData test = TableData( "me", 3,3);
test.data[0][0]="test2222";
std::cout<<test.data[0][0];
}
//示例程序
#包括
#包括
#包括
类表数据
{
公众:
TableData(标准::字符串名称,int行,int列):tableName(名称),rowCount(行),columnCount(列)
{
数据。调整大小(列数);
对于(int i=0;i std::cout我已经设法让代码正常工作。正如@dratenik提到的,问题与默认复制构造函数中发生的浅层复制有关。为了修复它,我在TableData类中添加了一个移动构造函数
class TableData
{
public:
TableData(std::string name, int rows, int columns) : tableName(name), rowCount(rows), columnCount(columns)
{
data = new std::string*[rowCount];
for (int count = 0; count < rowCount; ++count)
data[count] = new std::string[columnCount];
columnID.resize(columnCount);
}
TableData(TableData&& t) : rowCount(t.rowCount), columnCount(t.columnCount)
{
tableName=t.tableName;
data = new std::string*[rowCount];
for (int count = 0; count < rowCount; ++count)
data[count] = new std::string[columnCount];
for (int r_count = 0; r_count < rowCount; ++r_count)
for (int c_count = 0; c_count < columnCount; ++c_count)
data[r_count][c_count] = t.data[r_count][c_count];
}
~TableData()
{
for (int count = 0; count < rowCount; ++count)
delete[] data[count];
delete[] data;
}
std::string **data;
std::string tableName = "";
const int rowCount;
const int columnCount;
std::vector<std::string> columnID;
};
类表数据
{
公众:
TableData(标准::字符串名称,int行,int列):tableName(名称),rowCount(行),columnCount(列)
{
数据=新标准::字符串*[rowCount];
对于(int count=0;count
仔细阅读如何提供。问题可能出在您没有显示的代码中,因为您没有提供代码,所以任何人都能帮助您的唯一方法是猜测您没有显示的代码实际在做什么。胡乱猜测:push_back是使用默认的复制构造函数进行复制,它会进行浅复制,并立即进行复制当临时文件被销毁时会变成mush。要么定义一个合适的复制构造函数,要么使用emplace而不是push_back。你能在得到t的地方发布更多的代码吗
class TableData
{
public:
TableData(std::string name, int rows, int columns) : tableName(name), rowCount(rows), columnCount(columns)
{
data = new std::string*[rowCount];
for (int count = 0; count < rowCount; ++count)
data[count] = new std::string[columnCount];
columnID.resize(columnCount);
}
TableData(TableData&& t) : rowCount(t.rowCount), columnCount(t.columnCount)
{
tableName=t.tableName;
data = new std::string*[rowCount];
for (int count = 0; count < rowCount; ++count)
data[count] = new std::string[columnCount];
for (int r_count = 0; r_count < rowCount; ++r_count)
for (int c_count = 0; c_count < columnCount; ++c_count)
data[r_count][c_count] = t.data[r_count][c_count];
}
~TableData()
{
for (int count = 0; count < rowCount; ++count)
delete[] data[count];
delete[] data;
}
std::string **data;
std::string tableName = "";
const int rowCount;
const int columnCount;
std::vector<std::string> columnID;
};