我的分段错误发生在哪里?C
下面我有一些代码创建一个单链表并在其中存储一些点。它可以编译,但就我个人而言,我不知道在哪里以及为什么会出现分割错误。它发生在程序启动时 另外,在你告诉我之前,我知道这个排序链表算法效率很低,但我已经尝试让它工作了一段时间,现在我只是想让它在这一点上工作 非常感谢您的帮助 文件1:我的分段错误发生在哪里?C,c,segmentation-fault,C,Segmentation Fault,下面我有一些代码创建一个单链表并在其中存储一些点。它可以编译,但就我个人而言,我不知道在哪里以及为什么会出现分割错误。它发生在程序启动时 另外,在你告诉我之前,我知道这个排序链表算法效率很低,但我已经尝试让它工作了一段时间,现在我只是想让它在这一点上工作 非常感谢您的帮助 文件1: #include <assert.h> #include <stdlib.h> #include <stdio.h> #include "point.h" typedef str
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include "point.h"
typedef struct node
{
Point p;
struct node *next;
} node;
void insertAtFront(node **start, Point *ip);
void sort(node *start);
void swap(node *a, node *b);
void printList(node *start);
int main(int argc, char **argv)
{
Point *insertPoint;
node *head = NULL;
point_set(insertPoint,7.0,1.0);
insertAtFront(&head,insertPoint);
sort(head);
point_set(insertPoint,1.0,1.0);
insertAtFront(&head,insertPoint);
sort(head);
point_set(insertPoint,4.0,1.0);
insertAtFront(&head,insertPoint);
sort(head);
point_set(insertPoint,3.0,1.0);
insertAtFront(&head,insertPoint);
sort(head);
printList(head);
}
void insertAtFront(node **start, Point *ip)
{
node *node1 = (node*)malloc(sizeof(node));
node1->p = *ip;
node1->next = *start;
*start = node1;
}
void sort(node *start)
{
Point *tp1 = malloc(sizeof(Point));
Point *tp2 = malloc(sizeof(Point));
int swapped;
node *node1;
node *node2 = NULL;
double d1;
double d2;
if(node1 == NULL)
return;
do
{
swapped = 0;
node1 = start;
*tp1 = node1->p;
*tp2 = node1->next->p;
d1 = distanceFromOrigin(tp1);
d2 = distanceFromOrigin(tp2);
while(node1->next != node2)
{
if(d1 > d2)
{
swap(node1,node1->next);
swapped = 1;
}
node1 = node1->next;
}
node2 = node1;
}
while(swapped);
}
void swap(node *a, node *b)
{
Point *tp;
*tp = a->p;
a->p = b->p;
b->p = *tp;
}
void printList(node *start)
{
node *temp = start;
Point *tp = malloc(sizeof(Point));
printf("\n");
while(temp != NULL)
{
*tp = temp->p;
printf("Point: (%g,%g) - Distance: %g", point_getX(tp), point_getY(tp), distanceFromOrigin(tp));
temp = temp->next;
}
}
好的,正确的开始是正确的:
int main(int argc, char **argv)
{
Point *insertPoint; // this pointer points nowhere
node *head = NULL;
point_set(insertPoint,7.0,1.0); // still point_set dereferences it
调试器是怎么说的?如果您向我们显示错误消息,那可能是一个提示。请向我们显示Valgrind的报告介绍如何使用调试器以便能够自己发现类似的问题。在为insertPoint分配空间后,它仍然显示“分段错误”。我添加了一些printf()语句来进行一些肮脏的调试,在这里我创建了第一个insertPoint,但在出错之前它甚至没有到达它。在添加了“insertPoint=malloc(sizeof(Point));”之后,在尝试运行您可能怀疑的
insertafront
或排序后,它仍然会读取“Segmentation Fault”。在sort
中,当node1
没有值时,您有一个测试如果(node1==NULL)
,该测试已修复,但仍然执行。在我创建第一个插入点之前,我在程序的一开始就输入了一个“printf(“--”);在它到达printf之前,它会给我“Segmentation Fault”statement@drewfiss90:始终将调试内容记录到未缓冲的输出流中(如fprintf(stderr,“-”)
或在打印到输出流后刷新输出流(如printf(“--”);flush(标准输出)
。
#ifndef _POINT_H_
#define _POINT_H_
typedef struct Point
{
double x;
double y;
} Point;
void point_translate(Point *p, double x, double y);
double point_distance(const Point *p1, const Point *p2);
double distanceFromOrigin(const Point *p1);
static inline double point_getX(const Point *p)
{
return p->x;
}
static inline double point_getY(const Point *p)
{
return p->y;
}
static inline Point *point_set(Point *p, double x, double y)
{
p->x = x;
p->y = y;
return p;
}
#endif
int main(int argc, char **argv)
{
Point *insertPoint; // this pointer points nowhere
node *head = NULL;
point_set(insertPoint,7.0,1.0); // still point_set dereferences it