我正在尝试调整一段代码来获取某些输入数据的FFT。一切顺利,转变。当我尝试将转换写入二进制文件时,我的问题就出现了:
FFT.exe中0x68d02424(msvcr100d.dll)的未处理异常:0xC0000005:访问冲突读取位置0x00161000。
最奇怪的是,每次编译和运行程序时都不会发生这种情况。大约每三次运行一次就好并将数据保存到文件中。
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdlib>
#include "dft/FFT.h"
#include "ffft/FFTReal.h"
int main ()
{
using namespace std;
const char* dataFile = "C:/users/gad18/documents/temp/testData.dat";
const char* transformFile = "C:/users/gad18/documents/temp/DFTtestOutput.dat";
int length;
off_t size;
fstream inFile;
inFile.open( dataFile, ios_base::in | ios_base::binary );
if ( ! inFile.is_open() )
{
cout << "Error opening " << dataFile << " for reading." << endl;
cout << "Terminating process." << endl;
cin.get();
exit( EXIT_FAILURE );
}
cout << "Reading from " << dataFile << endl;
inFile.seekg( 0, ios_base::end );
size = static_cast <off_t> ( inFile.tellg() );
inFile.seekg( 0, ios_base::beg );
length = size / sizeof( float );
float* buffer = new float[ length ];
float* F = new float [length ];
inFile.read( (char*) buffer, length * sizeof( float ) );
inFile.close();
cout << size << " bytes read in." << endl;
cout << length << " float elements read into memory." << endl;
cout << "Press return key to creat DFT object..." << endl;
cin.sync();
cin.get();
cout << "Creating DFT object of length " << length << " points." << endl;
dft::FFT dft_object( length );
ffft::FFTReal<float> ffft_object( length );
int bits_out = dft_object.get_bits();
cout << "Successfully created DFT object with " << bits_out << " bit depth." << endl;
cout << "Press return key to attempt fast Fourier transform of data..." << endl;
cin.sync();
cin.get();
dft_object.compute_FFT( F, buffer );
cout << "Press return key to save transform data..." << endl;
cin.sync();
cin.get();
fstream outFile;
outFile.open( transformFile, ios_base::out | ios_base::trunc | ios_base::binary );
if ( ! outFile.is_open() )
{
cout << "Error opening " << dataFile << " for writing." << endl;
cout << "Terminating process." << endl;
cin.get();
exit( EXIT_FAILURE );
}
else
{
outFile.write( (char*) &F, length * sizeof( float ) );
size = outFile.tellg();
cout << "Wrote " << size << " bytes to " << transformFile << endl;
outFile.close();
}
delete [] buffer;
delete [] F;
cout << "Press return key to continue...";
cin.sync();
cin.get();
return 0;
} // int main()
如果这不是在问题中包含代码的正确方法,我很抱歉(这里的第一篇文章)。
异常似乎一直发生在streambuf的第202行(可能有帮助吗?)。
真正让我失望的是,它似乎是随机发生的,因为这是我第一次遇到这个问题,我不知道在哪里开始寻找这个bug。根据有关此异常的其他帖子,我使用指针似乎有问题吗?
对于解决方案或如何处理错误的任何帮助都将非常感激。
谢谢你, 格雷格
答案 0 :(得分:2)
我认为问题出在这里:outFile.write( (char*) &F, length * sizeof( float ) );
我认为你想要的是:
outFile.write( (char*) F, length * sizeof( float ) );
您正在从指针的地址开始转储内存,而不是指针中存储的地址。如果这两个地址在内存中具有正确的关系,则写入将起作用,尽管文件中的某些数据将是错误的。但是这种行为是不可预测的,并且可能导致崩溃。
如果我是你,我会考虑一种比代表输出数组的内存转储更强大的文件格式 -
答案 1 :(得分:1)
由于您在Windows机器上,请尝试从MSDN下载Application Verifier。 (对不起,从我的WP7输入这个,所以我没有方便的链接)。启用pageheap并启用基本检查后,它可能会精确确定问题。