我的程序有时只会得到一个Segmentation fault: 11,我无法弄清楚我的生活.我不太了解C++和指针领域,所以我应该寻找什么样的东西?
我知道它可能与我正在使用的一些函数指针有关.
我的问题是什么样的东西会产生分段错误?我拼命地迷失了,我已经查看了所有可能导致这种情况的代码.
我正在使用的调试器是lldb,它显示了此代码段中的错误:
void Player::update() {
// if there is a smooth animation waiting, do this one
if (queue_animation != NULL) {
// once current animation is done,
// switch it with the queue animation and make the queue NULL again
if (current_animation->Finished()) {
current_animation = queue_animation;
queue_animation = NULL;
}
}
current_animation->update(); // <-- debug says program halts on this line
game_object::update();
}
Run Code Online (Sandbox Code Playgroud)
current_animation并且queue_animation都是阶级的指针Animation.
另外需要注意的是,within Animation::update()是一个在构造函数中传递给Animation的函数指针. …
我正在尝试修改main(tab)中声明的char数组.所以我将它发送到modify_tab并修改它.但它不起作用并向我发送分段错误.这是我的代码:
1?void my_putstr(char *str)
2?{
3? int i;
4?
5? i = 0;
6? while (str[i] != '\0')
7? {
8? write(1, &str[i], 1);
9? i++;
10? }
11?}
12?
13?void modify_tab(char *tab)
14?{
15? char *map;
16?
17? map = tab;
18? map[3] = 'a';
19? my_putstr(map);
20?}
21?
22?void main()
23?{
24? char *tab;
25?
26? tab = "0123456789\n\0";
27? my_putstr(tab);
28? modify_tab(tab);
29?}
Run Code Online (Sandbox Code Playgroud) 下面的代码给了我一个分段错误,我尝试了很多不同的东西,但无法理解为什么它不起作用.我对C++很陌生,所以如果这个问题可能有点容易,我很抱歉;)这是我的代码:
class Line2D
{
public:
Point2D punkt;
double dx, dy;
void set_values(Point2D p, double d1, double d2);
Point2D* getIntersectionPoint(Line2D& line2);
};
void Line2D::set_values(Point2D p, double d1, double d2)
{
punkt = p;
dx = d1;
dy = d2;
}
Point2D* Line2D::getIntersectionPoint(Line2D& line2)
{
double a, b;
Point2D* intersec;
double det = dx*line2.dy - dy*line2.dx;
if(det == 0)
{
return NULL;
}
else
{
double c1 = line2.punkt.k1 - punkt.k1;
double c2 = line2.punkt.k2 - punkt.k2;
a = (line2.dy*c1 - line2.dx*c2)/det; …Run Code Online (Sandbox Code Playgroud) 码:
#include <iostream>
using namespace std;
void mix(int *a, int *b, int *c, int m, int n)
{
int i, j = 0;
for(i = 0; i < m; i++)
{
if(a[i] % 2 == 0)
{
c[j] = a[i];
j++;
}
}
for(i = m - 1; i >= 0; i++)
{
if(a[i] % 2 == 1)
{
c[j] = a[i];
j++;
}
}
for(i = 0; i < n; i++)
{
if(b[i] % 2 == 0)
{
c[j] …Run Code Online (Sandbox Code Playgroud) 我正在考虑一下C编程语言,并开始想知道如何inline与递归交互.我做了这个测试程序来找出答案.
static inline void f(void) {
f();
}
int main(void) {
f();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我使用编译程序gcc并且根本没有警告
$ c99 -Wall -pedantic main.c -o a
Run Code Online (Sandbox Code Playgroud)
我的问题是
关于混合
inline递归的C标准有什么用?
对我来说,不应该允许它是合乎逻辑的.
因此,我的代码可以很好地编译,但是每当我尝试运行该程序时,它就会出现段错误。我通过gdb运行它并得到了这组错误:
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_S_construct null not valid
Program received signal SIGABRT, Aborted.
0x00007ffff722bcc9 in __GI_raise (sig=sig@entry=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
56 ../nptl/sysdeps/unix/sysv/linux/raise.c: No such file or directory.
Run Code Online (Sandbox Code Playgroud)
这是我的代码,如果有帮助的话:
using namespace std;
#include <iostream>
#include <vector>
#include <cmath>
#include<fstream>
#include <cstdlib>
#include <utility>
#include <string>
class Page
{
public:
int TimeStamp;
int ProgramOwner;
int LocalPageNumber;
};
//Pair: first entry is local page, second entry is global page
//Second entry: 1 if in main memory, 0 if …Run Code Online (Sandbox Code Playgroud) 我的程序有以下代码
int main(void)
{
int i,size;
float vel_x[size],vel_y[size],vel_x0[size],vel_y0[size],rho[size],rho0[size];
float *ux, *vy, *ux0, *vy0;
float *r, *r0;
struct fdparam fdparam_1;
printf("Enter the number of grid points: \t");
scanf("%d", &fdparam_1.N);
printf("Enter the maximum number of iterations: \t");
scanf("%d", &fdparam_1.MAXIT);
printf("Enter the value for time domain: \t");
scanf("%f", &fdparam_1.t_domain);
printf("Enter the time step and density of the fluid: \t \t");
scanf("%f\t%f", &fdparam_1.Dt, &fdparam_1.dens);
printf("Enter the diffusion coefficient and viscosity: \t \t");
scanf("%f\t%f",&fdparam_1.diff, &fdparam_1.mu);
printf("The parameters are N=%d, MAXIT=%d, t_domain=%f, Dt=%f, diff=%e, mu=%e, dens=%f …Run Code Online (Sandbox Code Playgroud) 我有以下代码的分段错误,我真的不明白问题在哪里...
int *p;
p[0]=1;
printf("%d\n",*p);
Run Code Online (Sandbox Code Playgroud)
谢谢
这是我试图运行的代码 -
typedef struct node{
string str;
}node;
string s = "";
node *t = (node *)malloc(sizeof(node));
t->str = s ; // if s is empty, this statement gives segmentation fault.
cout<<t->str<<endl;
Run Code Online (Sandbox Code Playgroud) 以下是我的demo.c文件:
main;
Run Code Online (Sandbox Code Playgroud)
在编译这个gcc时会发出警告:
demo.c:1:1:警告:数据定义没有类型或存储类[默认启用]
运行./a.out会出现分段错误:
分段故障(核心转储)
是因为,(1)main没有在任何地方定义,我们正在尝试执行它;(2)我们在任何函数之外使用命令语句,因此它无法执行.
在任何一种情况下,我仍然不明白为什么它应该抛出一个段错误.
更新:它可能看起来类似于Is'int main;' 一个有效的C/C++程序?,但这是不同的,因为不使用任何标识符,编译代码.