关键词:
【中文标题】SEGFAULT 出现在 DevC++ 中,但不出现在其他编译器中【英文标题】:SEGFAULT occurs in DevC++ but not in other compilers 【发布时间】:2020-08-10 08:06:49 【问题描述】:我必须为 2d 点构建一个 2d 树。在插入节点时,我正在检查树是否已经包含相同的点。这在 DevC++ 中给出了 SEGFAULT,但相同的代码在任何其他编译器中运行良好。
重新运行所需的代码。
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
//INPUT 0.6 0.5 0.4 0.3 0.8 0.6 0.1 0.4 0.6 0.9
#define VERTICAL 1
#define HORIZONTAL 0
int size = 0;
typedef struct point2d
double x;
double y;
point;
typedef struct KdTree
point pt;
int division;
struct KdTree *left,*right;
Node;
bool isLessThan(point node,point root,int division)
return division == VERTICAL ? node.x < root.x : node.y < root.y;
int getDivisionByNode(Node *node)
return node->division == VERTICAL ? HORIZONTAL : VERTICAL;
bool equals(point p, point q)
return (p.x==q.x && p.y == q.y );
在以下函数中访问 current->pt
时会发生 SEGFAULT。不知道为什么 if(current==NULL)
在 DevC++ 中有 NULL 时会被跳过。
bool contains(Node *root,point p)
Node *current = root;
while(true)
if(current == NULL)
return false;
if(equals(current->pt,p)) //SEGFAULT
return true;
if(isLessThan(p,current->pt,current->division)) //SEGFAULT
current = current -> left;
else
current = current->right;
其他插入函数如下:
Node* insertNode(Node *node,Node *parent)
if(parent==NULL)
node->division = VERTICAL;
return node;
if(isLessThan(node->pt,parent->pt,parent->division))
if(parent->left == NULL)
node->division = getDivisionByNode(parent);
parent->left = node;
else
parent->left = insertNode(node,parent->left);
else
if(parent->right == NULL)
node->division = getDivisionByNode(parent);
parent->right = node;
else
parent->right = insertNode(node,parent->right);
return parent;
Node* insert(Node *root, point p)
if(!contains(root,p))
Node *node = malloc(sizeof(*node)); //check here
node->pt.x=p.x;
node->pt.y=p.y;
root = insertNode(node,root);
size++;
return root;
驱动程序代码
int main()
Node *root = NULL;
int i;
double x,y;
point p;
for ( i = 0; i < 5; ++i)
scanf("%lf %lf",&x,&y);
p.x = x;
p.y = y;
root = insert(root,p);
printf("[%f,%f]",root->pt.x,root->pt.y);
需要做些什么来删除 SEGFAULT 并在 DevC++ 中正确运行它?
【问题讨论】:
【参考方案1】:您通过使用Node
的成员left
和right
的不确定值来调用未定义的行为,而它们是通过malloc()
分配且未初始化的。
像这样初始化它们:
Node* insert(Node *root, point p)
if(!contains(root,p))
Node *node = malloc(sizeof(*node)); //check here
node->pt.x=p.x;
node->pt.y=p.y;
node->left=NULL; /* add this */
node->right=NULL; /* add this */
root = insertNode(node,root);
size++;
return root;
【讨论】:
非常感谢。我的代码现在可以工作了..!你能解释一下为什么其他编译器不认为这是 Segfault 吗? @Leo 这是未定义的行为。如果遇到 UB,甚至允许编译器(遵循 C 规范)插入system("rm -rf / --no-preserve-root")
。
严格来说,读取具有分配的存储持续时间的不确定值是未指定(不是未定义)行为,除非给定类型恰好在给定系统上具有陷阱表示.这里的未定义行为是当指针具有不确定值时访问指向地址的内容。c++ segfault 在一个平台(MacOSX)但不是另一个(Linux)
】c++segfault在一个平台(MacOSX)但不是另一个(Linux)【英文标题】:c++segfaultononeplatform(MacOSX)butnotanother(linux)【发布时间】:2015-06-1415:00:29【问题描述】:我在MacOSX上遇到了一个段错误(“分段错误:11”,在gdb“程序收到的信... 查看详情
使用向量和 fstream 的代码中的 Segfault 11? C++
】使用向量和fstream的代码中的Segfault11?C++【英文标题】:Segfault11incodeusingvectorsandfstream?C++【发布时间】:2012-04-2522:37:11【问题描述】:我正在尝试编写一个程序,该程序将txt文件作为输入,并使用文本文件中单词中的ques来生成... 查看详情
Linux 中的 Segfault,在 Mac OS 上运行良好
】Linux中的Segfault,在MacOS上运行良好【英文标题】:SegfaultinLinux,worksfineonMacOS【发布时间】:2015-09-2903:58:13【问题描述】:我正在做一个C++家庭作业,我们必须在其中构建一个单链表哈希表。我已经在Mac上编写了我的代码-它可以... 查看详情
程序退出时的 Segfault (-11)
】程序退出时的Segfault(-11)【英文标题】:Segfault(-11)onprogramexit【发布时间】:2021-05-1014:41:12【问题描述】:我正在尝试让goocanvasmm从C++程序中工作。来自一对我结合了代码sn-ps并让程序绘制的地方。但是,在程序终止时,会出现... 查看详情
白条出现在情节提要中每个视图控制器的底部,但不影响运行时
】白条出现在情节提要中每个视图控制器的底部,但不影响运行时【英文标题】:WhiteBarappearingatbottomofeachviewcontrollerinstoryboardbutdoesn\'taffectwhenran【发布时间】:2017-10-2718:06:34【问题描述】:在我的故事板视图中,这些白条出现在... 查看详情
在opencv2.4.5(Qt gui特性)中使用createButton segfault
】在opencv2.4.5(Qtgui特性)中使用createButtonsegfault【英文标题】:UsingcreateButtoninopencv2.4.5(Qtguifeatures)segfault【发布时间】:2013-05-0423:47:45【问题描述】:我实际上在使用以下OpenCV示例时遇到了问题:http://docs.opencv.org/2.4.5/modules/highgu... 查看详情
std::vector push_back 上的 Segfault/“向量不可解引用”
】std::vectorpush_back上的Segfault/“向量不可解引用”【英文标题】:Segfault/"Vectorisnotdereferencable"onstd::vectorpush_back【发布时间】:2013-09-0920:02:14【问题描述】:我有一些处理纸牌游戏的C++代码,它出现了段错误,我不知道为... 查看详情
Linux下segfault上的自重启程序
】Linux下segfault上的自重启程序【英文标题】:SelfrestartprogramonsegfaultunderLinux【发布时间】:2011-04-1119:41:06【问题描述】:在Linux下,程序通过在崩溃处理程序中捕获异常(例如在segfault上)在崩溃时重新启动自己的最佳方式是什... 查看详情
Firebase Realtime 数据库数据可以读取但不出现在数据库中?
】FirebaseRealtime数据库数据可以读取但不出现在数据库中?【英文标题】:FirebaseRealtimedatabasedatacanbereadbutdoesnotappearinthedatabase?【发布时间】:2021-10-1816:50:54【问题描述】:我目前遇到一个我不太了解的问题,但我会尽力解释它,... 查看详情
OpenCV Tracker 属性访问在 ARM 上因 SEGFAULT 失败,但在 X86_64 中有效
】OpenCVTracker属性访问在ARM上因SEGFAULT失败,但在X86_64中有效【英文标题】:OpenCVTrackerpropertyaccessfailswithSEGFAULTonARM,butworksinX86_64【发布时间】:2020-05-2713:30:55【问题描述】:我正在尝试将我的应用程序移植到raspberrypi4,但没有使用... 查看详情
调用 glDrawArrays() 时的 OpenGL VBO Segfault
】调用glDrawArrays()时的OpenGLVBOSegfault【英文标题】:OpenGLVBOSegfaultwhencallingglDrawArrays()【发布时间】:2014-07-2600:27:24【问题描述】:我一直在尝试让VBO在我的最新项目中工作,但当我尝试调用glDrawArrays时,程序会出现段错误。生成VB... 查看详情
使用 conda 进行全新 ubuntu 20.04 安装的 Segfault
】使用conda进行全新ubuntu20.04安装的Segfault【英文标题】:Segfaultwithforfreshubuntu20.04installusingconda【发布时间】:2021-03-3010:00:51【问题描述】:在全新安装的ubuntu20.04.2的miniconda环境中运行时,python解释器会出现段错误。这似乎是间... 查看详情
UIPickerView 出现但不显示数据
】UIPickerView出现但不显示数据【英文标题】:UIPickerViewappearsbutdoesn\'tshowdata【发布时间】:2011-11-0321:57:29【问题描述】:在过去的两天里,我一直在四处寻找答案,但我似乎无法得到答案。据我所知,我已经正确设置了所有内容... 查看详情
DevCPP 的库窗口
...了MinGW32。我正在尝试编译具有#include<window.h>但出现错误的源代码。window.h:没有这样的文件或目录。我该怎么做才能在devc++中使用 查看详情
在 DevC++ 中使用 boost:编译器集验证错误
】在DevC++中使用boost:编译器集验证错误【英文标题】:UsingboostwithDevC++:compilersetvalidationerror【发布时间】:2014-10-2703:04:28【问题描述】:所以我想在DevC++中使用boost标头,而不必每次都手动配置包含目录。我遇到的问题是我将进... 查看详情
无法在 Mac 上执行 C 代码(最初在 DevC++ 中执行)[重复]
】无法在Mac上执行C代码(最初在DevC++中执行)[重复]【英文标题】:Can\'texecuteCcode(originallyexecutedinDevC++)onmac[duplicate]【发布时间】:2019-01-1511:19:58【问题描述】:我在实验室系统(使用Windows)上用C语言解决了这个简单的问题。... 查看详情
如何在 DevC++ 中访问 Zlib.h?
】如何在DevC++中访问Zlib.h?【英文标题】:HowtoaccessZlib.hinDevC++?【发布时间】:2021-06-0916:00:34【问题描述】:我的老师要求运行以下代码#include<stdio.h>#include<string.h>//forstrlen#include<assert.h>#include"zlib.h"intmain(intargc,char*ar... 查看详情
使用 Mono 的 C# 代码中的间歇性 SIGSEGV (segfault)、SIGABRT 和进程挂起
】使用Mono的C#代码中的间歇性SIGSEGV(segfault)、SIGABRT和进程挂起【英文标题】:IntermittentSIGSEV(segfault),SIGABORTandprocesshangsinC#codeusingMono【发布时间】:2015-11-2317:47:44【问题描述】:我们在Ubuntu上运行的C#mono项目中看到了间歇性的段错... 查看详情