Note that there are some explanatory texts on larger screens.

plurals
  1. POIn consistent "Access violation reading location" while writing to binary file using fstream
    text
    copied!<p>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:</p> <blockquote> <p>Unhandled exception at 0x68d0ca24 (msvcr100d.dll) in FFT.exe: 0xC0000005: Access violation reading location 0x00161000.</p> </blockquote> <p>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.</p> <pre><code>#include &lt;iostream&gt; #include &lt;fstream&gt; #include &lt;cmath&gt; #include &lt;cstdlib&gt; #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 &lt;&lt; "Error opening " &lt;&lt; dataFile &lt;&lt; " for reading." &lt;&lt; endl; cout &lt;&lt; "Terminating process." &lt;&lt; endl; cin.get(); exit( EXIT_FAILURE ); } cout &lt;&lt; "Reading from " &lt;&lt; dataFile &lt;&lt; endl; inFile.seekg( 0, ios_base::end ); size = static_cast &lt;off_t&gt; ( 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 &lt;&lt; size &lt;&lt; " bytes read in." &lt;&lt; endl; cout &lt;&lt; length &lt;&lt; " float elements read into memory." &lt;&lt; endl; cout &lt;&lt; "Press return key to creat DFT object..." &lt;&lt; endl; cin.sync(); cin.get(); cout &lt;&lt; "Creating DFT object of length " &lt;&lt; length &lt;&lt; " points." &lt;&lt; endl; dft::FFT dft_object( length ); ffft::FFTReal&lt;float&gt; ffft_object( length ); int bits_out = dft_object.get_bits(); cout &lt;&lt; "Successfully created DFT object with " &lt;&lt; bits_out &lt;&lt; " bit depth." &lt;&lt; endl; cout &lt;&lt; "Press return key to attempt fast Fourier transform of data..." &lt;&lt; endl; cin.sync(); cin.get(); dft_object.compute_FFT( F, buffer ); cout &lt;&lt; "Press return key to save transform data..." &lt;&lt; endl; cin.sync(); cin.get(); fstream outFile; outFile.open( transformFile, ios_base::out | ios_base::trunc | ios_base::binary ); if ( ! outFile.is_open() ) { cout &lt;&lt; "Error opening " &lt;&lt; dataFile &lt;&lt; " for writing." &lt;&lt; endl; cout &lt;&lt; "Terminating process." &lt;&lt; endl; cin.get(); exit( EXIT_FAILURE ); } else { outFile.write( (char*) &amp;F, length * sizeof( float ) ); size = outFile.tellg(); cout &lt;&lt; "Wrote " &lt;&lt; size &lt;&lt; " bytes to " &lt;&lt; transformFile &lt;&lt; endl; outFile.close(); } delete [] buffer; delete [] F; cout &lt;&lt; "Press return key to continue..."; cin.sync(); cin.get(); return 0; } // int main() </code></pre> <p>My apologies if this is not the proper way to include code in a question (first post here).</p> <p>The exception seems to consistently occur at line 202 of streambuf (maybe that helps?).</p> <p>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?</p> <p>Any help as to a solution or how to handle running down there bugs would be much appreciated.</p> <p>Thank you, Greg</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload