在一致的“访问冲突读取位置”中使用 fstream 写入二进制文件时

发布于 2024-11-17 22:19:31 字数 3041 浏览 4 评论 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 行(也许这有帮助?)。

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

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

谢谢你, 格雷格

I'm attempting to adapt a piece of code to take the FFT of some input data. Everything goes fine taking the transform. My problem occurs when I try to write the transform to a binary file at which point I get:

Unhandled exception at 0x68d0ca24 (msvcr100d.dll) in FFT.exe: 0xC0000005: Access violation reading location 0x00161000.

Weirdest thing is, it doesn't happen every time I compile and run the program. About every third time it runs just fine and saves the data to the file.

#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()

My apologies if this is not the proper way to include code in a question (first post here).

The exception seems to consistently occur at line 202 of streambuf (maybe that helps?).

What's really throwing me off is that it is seemingly random in occurrence and since this is the first time I've run into this problem I'm not sure where to even start looking for the bug. Based on other posts about this exception, it seems there may be a problem with my use of pointers?

Any help as to a solution or how to handle running down there bugs would be much appreciated.

Thank you,
Greg

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉城已无爱 2024-11-24 22:19:31

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

我认为你想要的是:

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

你正在转储从指针地址开始的内存,而不是从指针中存储的地址开始转储内存。如果这两个地址在内存中具有正确的关系,则写入将有效,尽管文件中的某些数据可能是错误的。但这种行为是不可预测的,并且可能导致崩溃。

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

I believe the problem is here:outFile.write( (char*) &F, length * sizeof( float ) );

I think what you want is:

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

You are dumping memory starting at the address of the pointer, not of the address stored in the pointer. If these two addresses have the right relationship in memory, the write will work, although some of the data in the file will be wrong. But the behavior is unpredictable, and can cause a crash.

If I were you, I would think about a file format that is more robust than a dump of the memory representing your output array--

ぶ宁プ宁ぶ 2024-11-24 22:19:31

由于您使用的是 Windows 机器,请尝试从 MSDN 下载 Application Verifier。 (抱歉,这是从我的 WP7 上输入的,所以我手边没有链接)。启用页面堆和基本检查后,它可以准确地查明问题。

Since you're on a Windows box, try downloading Application Verifier from MSDN. (Sorry, typing this from my WP7 so I don't have the link handy). With pageheap and the basic checks enabled, it may pinpoint exactly the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文