在运行时调试 [英] Debugging at runtime
问题描述
我是一个完全的初学者,编写了我的第二个程序,最后
得到它编译,我有一个错误我可以'跟踪发生在
运行时间 - 基本上无论我做什么都说分段错误。
我想知道是否有任何方法我可以得到更多
详细描述错误,以便我可以考虑对它进行排序
out。
有人向我提到g ++选项-DRANGE_CHECKING将
检查我是否正在访问未初始化的数组或某些东西在编译
时间,但这没有帮助,在此先感谢,我真的在我的智慧
结束!
Hi,
I''m a complete beginner, having written my second program and finally
got it to compile, I''ve got an error I can''t track down occurring at
runtime - basically whatever I do it says Segmentation Fault.
I was wondering if there was any way I could get a slightly more
verbose description of the error so that I could think about sorting it
out.
Someone mentioned to me that the g++ option -DRANGE_CHECKING would
check if i was accessing uninitialised arrays or something at compile
time, but that hasn''t helped, Thanks in advance, I''m really at my wits
end!
推荐答案
Cleverbum写道:
Cleverbum wrote:
我是一个完全的初学者,编写了我的第二个程序,最后得到了编译,我有一个错误,我无法追踪发生在
运行时 - 基本上无论我做什么都说分段错误。
I''m a complete beginner, having written my second program and finally
got it to compile, I''ve got an error I can''t track down occurring at
runtime - basically whatever I do it says Segmentation Fault.
这个新闻组只有资格讨论C ++源码;不是所有的各种
实现和工具,包括调试器。
你可以在这里发布一些来源 - 大多数seg故障很容易发现 - 或者你/>
可以将关于调试的问题发布到g ++新闻组。
-
Phlip
http://c2.com/cgi/wiki?ZeekLand < - 不是博客!! !
This newsgroup is only qualified to discuss C++ source; not all its various
implementations and tools, including debuggers.
You could post some source here - most seg faults are easy to spot - or you
could post your question about debugging to a g++ newsgroup.
--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
似乎编译器新闻组只发布到每两天
左右,所以我不妨在这里发布代码,通过它几次我自己并没有找到任何不幸的事情:
/ *模型颗粒在三维空间内沉降。
还包括粒子间重力* /
#include< iostream>
#include< math.h>
#include< fstream>
#include< sstream>
#include< string >
#include< time.h>
using namespace std;
const int n = 1; //粒子数
const double G = 6.6742e-11; //万能引力常数
const double gravity = 9.81; //由于重力引起的自由落体加速
const int max_time = 100;
const int timeStep = 1;
struct vec_3d {
double x,y,z;
};
class particle {
//所有向量存储为数组[0] = x dir [1] = y dir [2] = z dir
私有:
double * pos;
double * vel;
double * frc;
双倍质量;
bool hard;
double rad;
public:
particle(){
srand(time(0));
int randint = 0;
for(int i = 0; i< 3; i ++){
randint = rand()%1000;
pos [i] = randint / 1000.0;
vel [i] = 0.0;
frc [i] = 0.0;
}
质量= 1.0;
rad = 1.0;
hard = true;
}
// void move(double *); //作为数组提供的速度
// void move(vec_3d); //作为矢量结构提供的速度
void move(); //使用粒子对象中保存的速度
// void acceleration(double *); //作为数组提供的力
// void accelerated(vec_3d); //作为向量提供的力
void accelerate(); //使用存储在粒子对象中的力
string toString(){
ostringstream os;
os<< pos [0]<< ''\t''
<< pos [1]<< ''\t''
<< pos [2]<< ''\t''
<< vel [0]<< ''\t''
<< vel [1]<< ''\t''
<< vel [2]<< ''\t'';
返回os.str();
};
friend void calculate_forces(particle ,粒子);
朋友无效single_particle_forces(粒子);
};
main(){
particle * myParticles = new particle [n];
for(int simulation_time = 0; simulation_time< max_time; simulation_time
+ = timeStep){
for(int i = 0; i< n; i ++){
if(n> 1){
for(int j = i + 1; j< n; j ++){
calculate_forces(myParticles [i],myParticles [j]);
}
}
single_particle_forces(myParticles [i]);
myParticles [i] .accelerate();
myParticles [i] .move();
cout<< i<< myParticles [i] .toString();
}
}
}
void particle :: move(){
for(int j = 0; j< 3; j ++){
pos [j] + = vel [j] * timeStep;
}
}
void particle :: accele(){
for (int j = 0; j< 3; j ++){
vel [j] + = frc [j] * timeStep;
}
}
void calculate_forces(粒子x,粒子y){
for(int i = 0; i< 3; i ++){
//身体之间的引力吸引力
int mySign; //用于确定力的方向
if(y.pos [i]> x.pos [i]){mySign = -1;} else {mySign = 1;}
x.frc [i] = mySign * G * x.mass * y.mass /((y.pos [i] - x.pos [i])*(
y.pos [i] - x.pos [i]));
y.frc [i] = -1 * mySign * G * x.mass * y.mass /((y。 pos [i] -
x.pos [i])*(y.pos [i] - x.pos [i]));
}
}
void single_particle_forces(粒子x){
//重力在z轴上呈负值
x.frc [2 ] - = x.mass * gravity;
}
seems the compiler newsgroups are only posted to every couple of days
or so, so I may as well post the code here, been through it a few times
myself and have had no luck in spotting anything untoward:
/* models particle(s) settling under gravity in three dimensions.
also includes inter-particle gravity */
#include <iostream>
#include <math.h>
#include <fstream>
#include <sstream>
#include <string>
#include <time.h>
using namespace std;
const int n = 1; // number of particles
const double G = 6.6742e-11; // universal gravitational constant
const double gravity = 9.81; // freefall acceleration due to gravity
const int max_time =100;
const int timeStep = 1;
struct vec_3d{
double x, y, z;
};
class particle{
// all vectors stored as arrays [0]=x dir [1]=y dir [2]=z dir
private:
double* pos;
double* vel;
double* frc;
double mass;
bool hard;
double rad;
public:
particle(){
srand(time(0));
int randint = 0;
for(int i=0;i<3;i++){
randint = rand()%1000;
pos[i] = randint / 1000.0;
vel[i]=0.0;
frc[i]=0.0;
}
mass=1.0;
rad=1.0;
hard=true;
}
//void move(double*); // speeds supplied as array
//void move(vec_3d); // speeds supplied as vector structure
void move(); // uses speeds saved within particle object
//void accelerate(double*); // forces supplied as array
//void accelerate(vec_3d); // forces supplied as vector
void accelerate(); // uses forces stored inside particle object
string toString(){
ostringstream os;
os << pos[0] << ''\t''
<< pos[1] << ''\t''
<< pos[2] << ''\t''
<< vel[0] << ''\t''
<< vel[1] << ''\t''
<< vel[2] << ''\t'';
return os.str();
} ;
friend void calculate_forces(particle, particle);
friend void single_particle_forces(particle);
};
main(){
particle* myParticles = new particle[n];
for(int simulation_time=0;simulation_time<max_time; simulation_time
+= timeStep){
for(int i=0; i<n; i++){
if(n>1){
for(int j=i+1; j<n; j++){
calculate_forces(myParticles[i], myParticles[j]);
}
}
single_particle_forces(myParticles[i]);
myParticles[i].accelerate();
myParticles[i].move();
cout << i << myParticles[i].toString();
}
}
}
void particle::move(){
for(int j=0;j<3;j++){
pos[j] += vel[j] * timeStep;
}
}
void particle::accelerate(){
for(int j=0;j<3;j++){
vel[j] += frc[j] * timeStep;
}
}
void calculate_forces(particle x, particle y){
for(int i=0;i<3;i++){
//Gravitational attraction between the bodies
int mySign; //used to determine direction of force
if(y.pos[i]>x.pos[i]){mySign = -1;} else {mySign = 1;}
x.frc[i] = mySign * G * x.mass * y.mass / (( y.pos[i] - x.pos[i])*(
y.pos[i] - x.pos[i]));
y.frc[i] = -1 * mySign * G * x.mass * y.mass / (( y.pos[i] -
x.pos[i])*( y.pos[i] - x.pos[i]));
}
}
void single_particle_forces(particle x){
//gravity acts negative on z axis
x.frc[2] -= x.mass * gravity;
}
Cl ******* @ hotmail.com 写道:
似乎编译新闻组只发布到每两天
或许,所以我也可以在这里发布代码,自己经历了几次,并且没有发现任何不幸的事情:
[...] >
类粒子{
//所有向量存储为数组[0] = x dir [1] = y dir [2] = z dir
pri vate:
double * pos;
double * vel;
double * frc;
double mass;
bool hard;
double rad;
public:
particle(){
srand(time(0));
int randint = 0;
for(int i = 0; i< 3; i ++){
randint = rand()%1000;
pos [i] = randint / 1000.0;
''pos''指针从未被初始化。你正在取消引用
一个未初始化(无效)的指针。
vel [i] = 0.0;
frc [i] = 0.0;
}
质量= 1.0;
rad = 1.0;
hard = true;
}
[...] >};
main(){
int main(){
[...]
seems the compiler newsgroups are only posted to every couple of days
or so, so I may as well post the code here, been through it a few
times myself and have had no luck in spotting anything untoward:
[...]
class particle{
// all vectors stored as arrays [0]=x dir [1]=y dir [2]=z dir
private:
double* pos;
double* vel;
double* frc;
double mass;
bool hard;
double rad;
public:
particle(){
srand(time(0));
int randint = 0;
for(int i=0;i<3;i++){
randint = rand()%1000;
pos[i] = randint / 1000.0;
''pos'' pointer has never been initialised. You''re dereferencing
an uninitialised (invalid) pointer.
vel[i]=0.0;
frc[i]=0.0;
}
mass=1.0;
rad=1.0;
hard=true;
}
[...]
};
main(){
int main(){
[...]
V
-
请在邮寄回复时从我的地址中删除资金
V
--
Please remove capital As from my address when replying by mail
这篇关于在运行时调试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!