运行时出现
引发了异常: 读取访问权限冲突。
this->data 是 0x1110112。
代码如下:
#pragma once
#include
#include
#include
using namespace std;
class CAudioRead
{
public:
CAudioRead():data(nullptr),length(0)
{
}
~CAudioRead()
{
if (data)
{
delete[] data;
data = nullptr;
}
}
void load(std::string &path);
int size() const { return length; }
short get(int pos);
private:
short *data;
int length;
};
inline void CAudioRead::load(std::string& path)
{
fstream fin;
fin.open(path.c_str(), ios::in | ios::binary);
if(fin)
{
fin.seekg(0, fin.end);
int len(fin.tellg());
fin.seekg(0, fin.beg);
char temp = new char[len];
fin.read(temp, len);
if (!fin) cerr << "failed" << endl;
data = new short[len / 2];
for (int i = 0; i < len / 2; ++i)
data[i] = *(reinterpret_cast<short>(temp + i * 2));
length = len;
delete[] temp;
}
}
inline short CAudioRead::get(int pos)
{
if (pos < 0 || pos >= length || data==nullptr)
cerr << "failed at" << FILE << " #" << LINE << endl;
return data[pos];
}
断点显示的是return data[pos]这一行