0

我正在尝试修改一段代码来获取一些输入数据的 FFT。一切顺利进行转换。当我尝试将转换写入二进制文件时会出现我的问题,此时我得到:

FFT.exe 中 0x68d0ca24 (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 行(也许有帮助?)。

真正让我失望的是它似乎是随机发生的,因为这是我第一次遇到这个问题,所以我什至不知道从哪里开始寻找错误。根据有关此异常的其他帖子,我使用指针似乎有问题?

任何有关解决方案或如何处理运行错误的帮助将不胜感激。

谢谢你,格雷格

4

2 回答 2

2

我相信问题出在这里:outFile.write( (char*) &F, length * sizeof( float ) );

我想你想要的是:

    outFile.write( (char*) F, length * sizeof( float ) );

您正在从指针的地址开始转储内存,而不是从存储在指针中的地址开始。如果这两个地址在内存中有正确的关系,写就可以了,虽然文件中的一些数据是错误的。但是这种行为是不可预测的,并且可能导致崩溃。

如果我是你,我会考虑一种比代表输出数组的内存转储更健壮的文件格式——

于 2011-06-29T20:51:14.233 回答
1

由于您使用的是 Windows 机器,请尝试从 MSDN 下载应用程序验证程序。(对不起,从我的 WP7 输入这个,所以我没有方便的链接)。启用 pageheap 和基本检查后,它可能会准确定位问题。

于 2011-06-29T21:15:31.590 回答