gpt4 book ai didi

c - 学习C,segfailt混淆

转载 作者:行者123 更新时间:2023-11-30 21:30:57 24 4
gpt4 key购买 nike

我一直在尝试在春假期间自学 C,这比我预期的更具挑战性!我已经取得了一些良好的进步,但我似乎遇到了障碍。我一直在编写自己的双向链表实现。经过大约八个小时的努力,我得到了一个看起来相当连贯的程序。编译器让我一切都清楚,并且我已经多次成功构建了该项目。现在,我开始用java编程;因此,我认为上述保证是成功的铁定保证。然而,C 似乎对事物的看法有所不同(请原谅双关语)!当我尝试运行我的程序时,出现“段错误”运行时错误。我花了一些时间阅读这个错误,并且(我确信我即将被告知)这是内存分配错误的问题。我尝试过调试,但无论我在哪里设置断点,我似乎都无法抢占错误。我有点左右为难,我真的很感激你们为我提供的任何见解!我对我刚刚潦草地写下的冗长的文字表示歉意……我会尽量保持帖子脚本简短。这是我的设置的粗略轮廓:

  • 具有 Cygwin 环境的 NetBeans 6.8 IDE
  • 当前运行 Windows 7 x64

我将在下面发布我的代码,但当然没有义务对其进行筛选。我真正希望的是关于如何处理一般段错误的一些建议。我感觉我会经常见到他们,我想制定一个策略来解决这个特定问题。

提前致谢!没有你们,我就会陷入困境!

main.c

#include <stdlib.h>
#include <stdbool.h>
#include"dll.h"

int main(int argc, char** argv) {

VECT coord1;
coord1->x = 0.0012345;
coord1->y = 0.012345;
coord1->z = 0.12345;

LIST coords = init_list();
list_add(coords, coord1);

return (EXIT_SUCCESS);
}

dll.c(双向链表源文件)

#include "dll.h"
#include <stdlib.h>
#include <string.h>
#include<stdbool.h>
#include"dll.h"

LIST init_list() {
LIST list = (LIST) malloc(sizeof (struct list));
list->length = 0;
list->first = NULL;
list->last = NULL;
list->destructor = free;
return list;
}

LIST_ITR list_iterator(LIST list, bool from_front) {
LIST_ITR iter = (LIST_ITR) malloc(sizeof (struct list_itr));
if (from_front) {
iter->current = list->first;
} else if (!from_front) {
iter->current = list->last;
} else return NULL;
iter->started = 0;
return iter;
}

void list_add(LIST list, VECT coords) {
NODE node = (NODE) malloc(sizeof (struct node));
node->coord_vector = coords;

if (list->first == NULL) {
node->prev = NULL;
node->next = NULL;
list->first = node;
list->last = node;
} else {
list->last->next = node;
node->prev = list->last;
node->next = NULL;
list->last = node;
}
list->length++;
}

VECT list_itr_current(LIST_ITR iter) {
if (iter->started && iter->current != NULL)
return iter->current->coord_vector;
else {
return NULL;
}
}

VECT list_itr_next(LIST_ITR iter) {
if (!iter->started && iter->current != NULL) {
iter->started = 1;
return iter->current->coord_vector;
}
if (iter->current != NULL) {
iter->current = iter->current->next;
return list_itr_current(iter);
}
return NULL;
}

VECT list_prev(LIST_ITR iter) {
if (!iter->started && iter->current != NULL) {
iter->started = 1;
return iter->current->coord_vector;
}
if (iter->current != NULL) {
iter->current = iter->current->prev;
return list_itr_current(iter);
}
return NULL;
}

VECT list_get_first(LIST list) {
return list->first->coord_vector;
}

VECT list_get_last(LIST list) {
return list->last->coord_vector;
}

VECT list_pop(LIST list) {
NODE last = list->last;
if (last == NULL) return NULL;

if (list->first == list->last) {
list->first = NULL;
list->last = NULL;
} else {
list->last = last->prev;
last->prev->next = NULL;
}

VECT data = last->coord_vector;
free(last);
list->length--;
return data;
}

VECT list_poll(LIST list) {
NODE first = list->first;

if (first == NULL)
return NULL;

if (list->first == list->last) {
list->first = NULL;
list->last = NULL;
} else {
list->first = first->next;
first->next->prev = NULL;
}

VECT data = first->coord_vector;
free(first);
list->length--;
return data;
}

void list_remove(LIST list, bool from_front) {
VECT data;
if (from_front)
data = list_poll(list);
else if (!from_front)
data = list_pop(list);
else return;
list->destructor(data);
}

void destroy_list(LIST list) {
NODE current = list->first;
NODE next;
while (current != NULL) {
next = current->next;
list->destructor(current->coord_vector);
free(current);
current = next;
}
free(list);
}

dll.h(双向链表头文件)

#include<stdbool.h>

#ifndef _DLL_H
#define _DLL_H

#ifdef __cplusplus
extern "C" {
#endif

/* A C implementation of a doubly-linked list. Contains void pointer values.
Can be used as a LIFO stack of FIFO queue. */

#define FRONT 0
#define BACK 1

struct vector{
double x;
double y;
double z;
};

typedef struct vector* VECT;

struct node{
VECT coord_vector;
struct node* next;
struct node* prev;
};

typedef struct node* NODE;

struct list{
int length;
NODE first;
NODE last;
void (*destructor)(void*);
};

typedef struct list * LIST;

struct list_itr{
NODE current;
char started;
};

typedef struct list_itr * LIST_ITR;

//Initializes the list
LIST init_list();

//initializes the list iterator
LIST_ITR list_iterator(LIST list, bool from_front);

//append element to end
void list_add(LIST list, VECT coords);

//Gets the data stored in the first item of the list or NULL if the list is empty
VECT list_get_first(LIST list);

//Gets the data stored in the last item of the list or NULL if the list is empty
VECT list_get_last(LIST list);

//LIFO pop: remove element and return data
VECT list_pop(LIST list);

//FIFO poll: remove element and return data
VECT list_poll(LIST list);

//Deletes element and frees memory
void list_remove(LIST list, bool from_front);

//Delete list and free all memory
void destroy_list(LIST list);

//returns the data of the element pointed to by current
VECT list_itr_current(LIST_ITR list_itr);

//Increments the index of current by 1 and returns the data stored there
VECT list_itr_next(LIST_ITR list_itr);

//Decrements the index of current by 1 and returns the data stored there
VECT list_prev(LIST_ITR list_itr);

#ifdef __cplusplus
}
#endif

#endif /* _DLL_H */

最佳答案

您应该使用 -Wall 标志编译器构建代码。在编译时它将打印:

main.c:9:15:警告:在此函数中使用“coord1”时未初始化 [-Wuninitialized]

这向您指出了问题所在。coord1 是一个指针类型,您可以分配给它,但 coord1 在初始化之前没有内存支持它。在下面的代码片段中,coord1 通过分配内存来存储其组件来初始化。这消除了段错误。

VECT coord1 = NULL;

coord1 = (VECT)malloc(sizeof(struct vector));
if (NULL == coord1)
{
fprintf(stderr, "Out of memory!\n");
exit(1);
}

coord1->x = 0.0012345;
coord1->y = 0.012345;
coord1->z = 0.12345;

通常,当程序访问操作系统尚未分配给它的内存时,就会发生段错误。未初始化的指针通常指向地址零,该地址未分配给任何程序。编译时始终使用 gcc -Wall,这会多次指出这些潜在问题。立刻帮我找到了。

此外,您还可以将 VECT 类型声明为 typedef 结构 vector (非指针类型)。

VECT coord1;
VECT* v_coord1 = &coord1;
v_coord1->x = 0.0012345;
v_coord1->y = 0.012345;
v_coord1->z = 0.12345;`

此外,变量命名约定也可以提供帮助。

struct vector{
double x;
double y;
double z;
};

typedef struct vector VECT;
typedef struct vector* pVECT;

关于c - 学习C,segfailt混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22160333/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com