10GeV
10GeV

Reputation: 475

Why is my code throwing a SIGBUS error, even when I store variables in heap?

In the method plotThermalNoise() of the Antenna class, for some reason the for loop does not run. Initially, I used int for n and i, however I need to work with much larger numbers than int can hold. SO, now I'm using a long int for both. The program no longer works, however. I stepped through it with GDB, and it seems I'm getting a SIGBUS error. I tried using new so as to store both variables in heap, however the loop still doesn't run.

#define k 0.0000000000000000000000138064852 //Boltzmann's constant

class Antenna{

    double _srate, _sdur, _res, _temp, _band;

    public:
        Antenna(double sampling_rate, double sample_duration, double resistance_ohms, double temperature_kelvin, double bandwidth_Hz){
            _srate = sampling_rate;     _sdur = sample_duration;
            _res   = resistance_ohms;   _temp = temperature_kelvin;
            _band  = bandwidth_Hz;
        }


        void plotThermalNoise();
};

void Antenna::plotThermalNoise(){

    //calculate RMS, mean of Gaussian
    double RMS =  sqrt(4 * _res * k * _temp * _band);
    double V   =  sqrt((4 * k * _temp * _band) / _res);

    long int n = _srate / _sdur;
    double x[*n],y[*n];
    
    gRandom->SetSeed(time(NULL));

    for(long int i = 0; i < n; ++i){

        x[i] = i;
        y[i] = gRandom->Gaus(V,RMS);

    }

    TGraph gr = new TGraph(n,x,y);
    gr->SetTitle("Thermal Noise Voltage vs Time Trace;Seconds;Volts");
    gr->Draw();

}

void dataAquisitionSim(){     

    Antenna test(4000000000, 0.000001, 50, 90, 500);
    test.plotThermalNoise();    

}

Upvotes: 0

Views: 230

Answers (1)

Employed Russian
Employed Russian

Reputation: 213526

    long int n = _srate / _sdur;
    double x[*n],y[*n];

This code will not compile. I assume your actual code is:

    long int n = _srate / _sdur;
    double x[n],y[n];

With the parameters you pass in: 4000000000 for _srate and 0.000001 for _sdur, n becomes 4,000,000,000 / 0.000,000,1 == 4,000,000,000,000,000.

You then attempt to allocate two double arrays of that size on stack. The total size of these arrays is 64 peta-bytes.

The largest super-computer currently in existence has "over 10PiB of memory". So you only need something mere 6 times larger than that.

it seems I'm getting a SIGBUS error.

As you should. Some back of the envelope calculations should help you realize that just because your code compiles doesn't mean it will run.

even when I store variables in heap?

Unless you actually have a computer with more than 64PiB of RAM, stack vs. heap is irrelevant -- you'll run out of either.

Upvotes: 1

Related Questions