I have written a small program designed to read file input and put this information into a multidimensional array. As it stands, the program reads just one file for now, the name of which I specify when calling readfile.open. However, I have now altered the program so that the readfile function accepts a c-style string (I need to do this so that in the future I can alter the name of the file by looping over variables). The program now crashes with the following message:

terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc

The program compiles fine; I am using g++ on red hat. Here is the program source code:

#include <cstdio>
#include <cstdlib>
#include <complex>
#include <math.h>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;

complex<double> convert_complex(complex<double>);

ifstream:os_type size;
complex<double> *zbuff;

int main(int argc, char *argv[])
{
int srcdir, srccol, slice, sinkpar;
int i, j, k, kount;
int Nc = 3;
int spaceh = (24*24*24)/2;
srcdir = 0;

complex<double> Psi[4][Nc][spaceh][4]; // Complex array to store info in file //

string filename;
stringstream(filename) << "bqcd.567.00000.00.1.1.10.0.prop"; // Filename, potentially modifiable //

ifstream readfile;
readfile.open(filename.c_str(), ios::in | ios::binary | ios::ate);
size = readfile.tellg(); // Determine size of input file using the get pointer //
readfile.seekg(0, ios::beg); // Put pointer back to beginning of file //
zbuff = new complex<double>[size]; // < -- Execution stops here. zbuff is simple a one dimensional complex array //
readfile.read(reinterpret_cast <char *> (zbuff), size);


for(k = 0; k < spaceh; k++)
{
for(j = 0; j < Nc; j++)
{
for(i = 0; i < 4; i++)
{
Psi[i][j][k][srcdir] = convert_complex(zbuff[kount]); // Take info in 1-d array zbuff, change endianness of entry, place in correct element of 4-d array Psi //
kount++;
}
}
}

readfile.close();

cout << Psi[1][1][1][srcdir] << endl; // Test output from Psi //
cout << Psi[2][1][2][srcdir] << endl;
cout << Psi[0][0][0][srcdir] << endl;
cout << Psi[0][0][790][srcdir] << endl;

return 0;
}

complex<double> convert_complex(complex<double> in)
{
complex<double> out;
char *p_in = (char *) &in;
char *p_out = (char *) &out;
p_out[0] = p_in[15];
p_out[1] = p_in[14]; // Function to reverse byte order of a single entry of complex array zbuff //
p_out[2] = p_in[13];
p_out[3] = p_in[12];
p_out[4] = p_in[11];
p_out[5] = p_in[10];
p_out[6] = p_in[9];
p_out[7] = p_in[8];
p_out[8] = p_in[7];
p_out[9] = p_in[6];
p_out[10] = p_in[5];
p_out[11] = p_in[4];
p_out[12] = p_in[3];
p_out[13] = p_in[2];
p_out[14] = p_in[1];
p_out[15] = p_in[0];
return out;
}


As I said, the program works perfectly when the filename is given explicitly. Only when I pass the filename as a string does the program crash. Curiously, the crash does not occur at the filename line, but later on, when "new" is called. The bas alloc suggests I have run out of space for something. I don't see how that is possible, since nothing is particularly large here. zbuff is the largest creation here, but there were no complaints before I made this minor change, and I also intent to make zbuff considerably larger in the future!

Any help would be much appreciated.

Warren