Unhandled exception

Hi there, I'm fairly new to this C++ thing so I hope you can help.

I'm writing a program that prints out the coordinates of atoms in a simulation box (30x30x30). I have a fair few header and .cpp files so I'll only paste the bits that are faulty. I basically set up a subroutine to print out the coordinates of the atoms, and then use a loop and a random number generator to "insert" particles into that box. Here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

// Subroutine to print a PDB of the coordinates
void print_pdb(double **coords, const int simbox_TotalNparticles, const int move)
{
    char filename[128];
    
    _snprintf(filename, 128, "output%000006d.pdb", move);

    FILE *f = fopen(filename, "w");

    fprintf(f, "CRYST1 %8.3f %8.3f %8.3f  90.00  90.00  90.00\n", 
               sim_box[0], sim_box[1], sim_box[2]);

    for (int i = 0; simbox_TotalNparticles; i = i + 1)
    {
        fprintf(f, "ATOM  %5d  Methane   Methane     1    %8.3f%8.3f%8.3f  1.00  0.00          Methane\n",
                       i+1, coords[i][0], coords[i][1], coords[i][2]); 
        fprintf(f, "TER\n");
   
	}

    fclose(f);
}

int main()
{
 // Random variables and other
        
double **coords = new double*[simbox_TotalNparticles];
	
int seedmain;

        // Random seed
        srand(time(NULL));
        seedmain=1; //time(NULL);
        
        // Starts the seqeunce of randon numbers
        CRandomMersenne RanGen(seedmain);
        CRandomMersenne *pRanGen;
        pRanGen=&RanGen;
		double random=RanGen.Random();

    // Randomly generate the coordinates of the atoms in the box
       for (int i = 0; i < simbox_TotalNparticles ; i = i + 1)
    {
        coords[i] = new double[3];

    // Note "random(0,x)" would generate a random number
    // between 0 and $x
        coords[i][0] = random*sim_box[0];
        coords[i][1] = random*sim_box[1];
        coords[i][2] = random*sim_box[2];
		
    // Print the initial PDB file
		
		print_pdb(coords, simbox_TotalNparticles, 0);
		cin.ignore();
	   
	   }
	   return (0);
}


It should be pointed out that the problem lies with the print_pdb function. The other variables you say there are defined in other files so don't worry about them. When I debug the program I get the following message:

"Unhandled exception at 0x775415de in new.exe: 0xC0000005: Access violation reading location 0xcdcdcddd."

It should be noted that if I comment out the final "// Print the initial PDB file" part at the end the program runs successfully.

Any ideas?
Cheers
for (int i = 0; simbox_TotalNparticles; i = i + 1)


I don't think that's what you meant.
Last edited on
for (int i = 0; simbox_TotalNparticles; i = i + 1)

I don't think that's what you meant.


In that it should be ?:

for (int i = 0; i < simbox_TotalNparticles; i = i + 1

Yeh I think you're right. However that doesn't seem to be what's causing the unhandled exception. The problem seems to lie with my use of "fopen", but I can't see why it should be a problem.
Print the filename before the fopen to make sure it's what you think it is. Check if the file pointer is null after the fopen - if it is find out why.
If fopen fails to open the file it returns null. Check if NULL was returned.
Cheers guys, there was a problem with "filename". It runs successfully now. On another note, as you can see here I'm using a mersenne twister to generate random numbers. I originally wrote the program to use the built in "rand" function in C++ which worked fine as a start, but I have doubts over how 'uniform' it truly is.

The original code took the form:

1
2
3
4
5
6
7
8
9
10
for (int i=0; i<simbox_TotalNparticles; i=i+1)
{

coords[i]=new double [3];

coords[i][0]=rand(0, sim_box[0]);
coords[i][0]=rand(0, sim_box[1]);
coords[i][0]=rand(0, sim_box[2]);

print_pdb(coords, simbox_TotalNparticles, 0);


which worked fine. The problem with the mersenne scheme I'm using (see original post) is that the random number generator seems to be resetting every time it loops. Is there some way round this (I'm obviously missing something obvious!)
To get a random number, you should call RanGen.Random(). How many times do you call this in your loop?
Yeh, I wasn't calling RanGen.Random() inside the loop, which meant it was using the initial random numbers for every iteration of the loop. Not exactly random...
Topic archived. No new replies allowed.