来自 for 循环的分段错误
Segmentation Fault from for loop
我有一个旧的C代码,我正在更新到C++。我已将所有数组(指向数组的指针(更改为向量。 此代码中涉及多个文件。相关部分如下。
当我运行它时,我在第一次迭代计算 fn.cpp 中的 m[i] 时出现分割错误错误。如果我在语句之外定义 m,在其减速时,我会得到 w[i] 上的错误。 我不确定如何解决这个问题
在主.cpp文件中:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <complex>
#include "fn.h"
using namespace std;
#include <gsl/gsl_rng.h>
// ===========================
// Variables
// ===========================
double eta = 0.13;
double w_max = 3;
int N_bath = 60;
vector<double> m(N_bath);
vector<double> c(N_bath);
vector<double> w(N_bath);
void main(){
bath_para(eta, w_max);
}
在 fn.h 文件中:
#ifndef FUNCTIONS
#define FUNCTIONS
extern int N_bath;
extern vector<double> c;
extern vector<double> m;
extern vector<double> w;
#endif
在 fn.cpp 文件中:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <complex>
#include <vector>
#include "fn.h"
using namespace std;
void bath_para(double eta, double w_max){
double w_0;
w_0 = (1 - exp(-w_max))/N_bath;
for (int i = 0; i < N_bath; ++i){
m[i] = 1.0
w[i] = -log(1-(i+1)*w_0);
c[i] = sqrt(eta*w_0*m[i])*w[i];
}
}
您可能在其他地方有错误。使用以下 fn.h 代码不会产生任何错误(添加了 both_para 和命名空间的声明,我假设您错误地将其删除为无关紧要(:
#ifndef FUNCTIONS
#define FUNCTIONS
using namespace std;
extern int N_bath;
extern vector<double> c;
extern vector<double> m;
extern vector<double> w;
void bath_para(double eta, double w_max);
#endif
相关文章:
- 在某些循环内使用vector.push_back时出现分段错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 当我的阵列太大时出现分段错误
- 分段错误当我试图运行程序时出错
- 在c++中初始化矩阵时出现分段错误(核心转储)
- 尝试使用集合函数时出现分段错误
- 我无法缩小此分段错误的原因
- g++的分段错误(在NaN上使用to_string两次时)
- 我是如何在这段代码中出现分段错误的
- 创建结构的数组时遇到分段错误
- 在c++中键入向量中的所有值后,得到分段错误(核心转储)
- 在 c++ 中实现 Trie 时出现分段错误
- 为什么 fstream 在打开带有格式的文件时会导致分段错误?
- 为什么我遇到分段错误?
- 动态类的分段错误(家庭作业问题)
- 分段错误 - 读取初始化指针的数组
- 如何摆脱C ++中的分段错误错误?
- 使用 CTYPE 时出现分段错误
- 为什么代码给出分段错误?