Leon
Leon

Reputation: 5

c++ file will not run but does compile

The file below will compile but not run, any advice?

The cursor will blink a couple of times then, stop. Don't know what the problem is?

run 1e7 samples for particles leaving the slab block. The idea is to take the average number of particles leaving the slab in the x direction.

#include <stdlib.h>
#include <stdio.h>
#include <math.h> 
#include <stdint.h>
#include <iostream>
#include <cstdlib> 

int main(int argc, char **argv)
{

    int i , J;
    int numhistories = 1E7;
    int num_batch = 1E3;
    char * num_abs;

    double  N1 = 0.25;
    double Smallsigma1 = 2.0;
    double N2 = 0.75;
    double Smallsigma2 = 0.5; 
    double Tsigma = (N1 * Smallsigma1) + (N2 * Smallsigma2);
    double xmin = 0.0; // Slab geometry 
    double xmax = 5.0; // Slab geometry 


    double x, c , phi;
    double  count = 0, total_num_abs = 0; 
    num_abs =  (char*) malloc (J+1); 
    if (num_abs==NULL) exit (1); 

    for (J = 0; J < num_batch; J++)
        {
            count = 0; 

            for (i = 1 ; i < numhistories ; i++)
                {
                    // randomly intialize the x location of a neutron 
                    x = xmax * (float)rand() / (float)(RAND_MAX) ;
                    // first interaction 
                    c = 2 * (float)rand() - 1 ; 
                    phi = (float)rand() / (float)RAND_MAX; 
                    x += -log(phi) * c / Tsigma;
                    while ((x < xmax) && (x > xmin)) 
                        {
                            phi = (float)rand() /(float)RAND_MAX; 
                            if (phi < 1 / Tsigma)
                                {
                                    count += 1 ; 
                                    break; 
                                }
                            c = 2 * (float)rand() - 1 ; 
                            phi = (float)rand() /(float)RAND_MAX; 
                            x += (-log(phi)) * c / Tsigma;
                        }
                }
            num_abs[J] = count;
            total_num_abs += num_abs[J];         
        }
    double mean = (double)total_num_abs / (double)(numhistories * num_batch);
    count = 0; 
    for (J = 0; J < num_batch; J++)
        {
            count += (num_abs[J] - numhistories * mean) * (num_abs[J] - numhistories *mean);
        }
    double variance = (double)count / (double)(num_batch * num_batch);

    printf("mean = %f, variance = %f\n", mean , variance);
    return 0;
}     

Upvotes: 1

Views: 63

Answers (1)

Mar Tijn
Mar Tijn

Reputation: 223

If it doesn't hit the print near the end of the program, and it doesn't seem to crash. Ergo it exits before. If you hook up a debugger and step through it, I'd suspect it end up exiting here:

num_abs =  (char*) malloc (J+1);
if (num_abs==NULL) exit (1); 

J seems to be uninitialized and can therefore be any value. And as suggested in the comments, please fire up your warning-level to -Wall or something like that. Warnings are there to help you prevent these type of problems.

If for some reason there is no debugger available, you can stick in a few more prints here in there to narrow down the location of the problem.

Good luck!

Upvotes: 2

Related Questions