我遇到了一个我不太了解的问题。 我正在Lubuntu的QTcreator中运行此代码,在代码之后说明了我遇到的问题。 这是一个简单的程序,使用分叉运行一些进程,这些分叉使用蒙特卡洛方法近似于Pi。
SIM.h
#include <complex>
#include <iostream>
#include <valarray>
const double PI = 3.141592653589793238460;
typedef std::complex<double> Complex;
typedef std::valarray<Complex> CArray;
// Cooley–Tukey FFT (in-place, divide-and-conquer)
// Higher memory requirements and redundancy although more intuitive
void fft(CArray& x)
{
const size_t N = x.size();
if (N <= 1) return;
// divide
CArray even = x[std::slice(0, N/2, 2)];
CArray odd = x[std::slice(1, N/2, 2)];
// conquer
fft(even);
fft(odd);
// combine
for (size_t k = 0; k < N/2; ++k)
{
Complex t = std::polar(1.0, -2 * PI * k / N) * odd[k];
x[k ] = even[k] + t;
x[k+N/2] = even[k] - t;
}
}
// Cooley-Tukey FFT (in-place, breadth-first, decimation-in-frequency)
// Better optimized but less intuitive
// !!! Warning : in some cases this code make result different from not optimased version above (need to fix bug)
// The bug is now fixed @2017/05/30
void fft(CArray &x)
{
// DFT
unsigned int N = x.size(), k = N, n;
double thetaT = 3.14159265358979323846264338328L / N;
Complex phiT = Complex(cos(thetaT), -sin(thetaT)), T;
while (k > 1)
{
n = k;
k >>= 1;
phiT = phiT * phiT;
T = 1.0L;
for (unsigned int l = 0; l < k; l++)
{
for (unsigned int a = l; a < N; a += n)
{
unsigned int b = a + k;
Complex t = x[a] - x[b];
x[a] += x[b];
x[b] = t * T;
}
T *= phiT;
}
}
// Decimate
unsigned int m = (unsigned int)log2(N);
for (unsigned int a = 0; a < N; a++)
{
unsigned int b = a;
// Reverse bits
b = (((b & 0xaaaaaaaa) >> 1) | ((b & 0x55555555) << 1));
b = (((b & 0xcccccccc) >> 2) | ((b & 0x33333333) << 2));
b = (((b & 0xf0f0f0f0) >> 4) | ((b & 0x0f0f0f0f) << 4));
b = (((b & 0xff00ff00) >> 8) | ((b & 0x00ff00ff) << 8));
b = ((b >> 16) | (b << 16)) >> (32 - m);
if (b > a)
{
Complex t = x[a];
x[a] = x[b];
x[b] = t;
}
}
//// Normalize (This section make it not working correctly)
//Complex f = 1.0 / sqrt(N);
//for (unsigned int i = 0; i < N; i++)
// x[i] *= f;
}
int main()
{
const Complex test[] = { 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0 };
CArray data(test, 8);
// forward fft
fft(data);
std::cout << "fft" << std::endl;
for (int i = 0; i < 8; ++i)
{
std::cout << data[i] << std::endl;
}
}
return 0;
}
SIM.cpp
#ifndef SIM_H
#define SIM_H
#include <random>
#include "SIM.h"
#include <math.h>
#include <ctime>
#include <iostream>
#include <chrono>
#include <fstream>
#include <string>
using namespace std;
class Sim{
private:
double xCoor, yCoor, resultPyth, inCircle, outsideCircle, approxPi, totalRead;
int runs, totalInsideCircle;
string FILENAME, FILENAME_END;
std::vector<double> piVector;
public:
Sim(){}
explicit Sim(int pRuns);
~Sim() = default;
bool isInCircle();
void calcPiAndPrint();
};
#endif
main.cpp
#include "SIM.h"
#include <chrono>
#include <ctime>
#include <fstream>
#include <iostream>
#include <math.h>
#include <stdio.h>
Sim::Sim(int pRuns) {
runs = pRuns;
}
bool Sim::isInCircle() {
std::random_device rd;
std::mt19937 mt(rd());
std::random_device rd1;
std::mt19937 mt1(rd1());
std::uniform_real_distribution<double> dist{-1.0, 1.0};
std::uniform_real_distribution<double> dist1{-1.0, 1.0};
for (int i = 0; i < runs; i++) {
double x = dist(mt);
double y = dist1(mt1);
xCoor = x;
yCoor = y;
resultPyth = sqrt(pow(x, 2) + pow(y, 2));
if (resultPyth <= 1.0) {
totalInsideCircle++;
}
}
return true;
}
void Sim::calcPiAndPrint() {
cout << "Inside circle: " << totalInsideCircle << endl; <-- a 10-digit number
cout << "Runs : " << runs << endl; //<-- a 10-digit number, should be 1000
cout << "Approximation of Pi = " << (totalInsideCircle / runs) * 4 << endl;
}
很抱歉,我尽可能地缩短了代码量。
所以要解决这个问题。创建对象时传递给构造函数的整数将保存在变量#include <iostream>
#include <string>
#include "Sim.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
using namespace std;
using std::cerr;
using std::endl;
int main () {
Sim *sim;
pid_t childPid;
pid_t pids[10];
for(int i = 0; i < 5; i++ ) {
if((pids[i] = fork()) < 0){
perror("failed to fork...");
return 1;
}
else if(pids[i] == 0){
sim= new Sim(1000);
sim->isInCircle();
exit(0);
}
else{
}
}
int n = 10;
int status;
pid_t pid;
while(n > 0){
pid = wait(&status);
sim->calcPiAndPrint();
printf("Child with pid %ld exited with status 0x%x.\n", (long)pid, status);
n--;
}
delete sim;
return 0;
}
中。在我的代码中,我将其设置为1000,但是当我调用int runs
时,它将打印一个10位数字。同样访问calcPiAndPrint()
将导致EXC_BAD_ACCESS错误,但也会打印10位数字。我通常在初始化变量时没有任何问题,但是我无法弄清楚这里出了什么问题。我确实怀疑我的叉子与问题有关。任何帮助表示赞赏。
答案 0 :(得分:3)
如果我理解正确,那么您在这里的操作如下:
Sim
对象,调用isInCircle()
方法,然后退出。sim->calcPiAndPrint()
。请注意,父进程从未创建Sim
对象。父进程中的指针sim
尚未初始化,因此尝试取消引用(例如,通过调用方法或尝试访问其未指向的对象的成员)将导致不确定的行为。在这种情况下,通常意味着它将尝试访问无效或垃圾内存,并且理想情况下会崩溃,以便您可以注意到您的错误……