学习C,避免混淆



春假期间,我一直在努力自学c,这比我预期的要困难得多!我取得了一些不错的进步,但我似乎遇到了障碍。我一直在编写自己的双链表实现。经过大约八个小时的努力,我得到了一个似乎相当连贯的程序。编译器为我提供了一切便利,我已经多次成功地构建了该项目。现在,我开始用java编程;因此,在我看来,上述保证是成功的铁腕保证。然而,C似乎对事物有不同的理解(请原谅双关语)!当我尝试运行我的程序时,我会得到一个"segfault"运行时错误。我花了一些时间阅读这个错误,(我确信我即将被告知)这是内存分配错误的问题。我试过调试,但无论我在哪里设置断点,我似乎都无法抢占错误。我有点不知所措,我真的很感激你们能为我提供任何见解!我为我刚刚潦草写下的一大堆文字道歉。。。我会尽量保持后脚本简短。以下是我的设置概要:

  • NetBeans 6.8 IDE与Cygwin环境
  • 当前运行windows 7 x64

我会在下面发布我的代码,但当然没有义务仔细筛选。我真正希望的是关于如何处理segfault的一些建议。我觉得我会经常见到他们,我想制定一个解决这个特殊问题的策略。

提前感谢!如果没有你们,我会陷入困境的!

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是通过分配内存来存储其组件来初始化的。这消除了segfault。

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;

通常,当程序访问操作系统没有分配给它的内存时,会发生segfault。未实例化的指针通常指向地址0,而地址0没有分配给任何程序。编译时总是使用gcc-Wall,这会多次指出这些潜在的问题。帮我马上找到了。

此外,您可以将VECT类型声明为typedefstructvector(非指针类型)。

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;

相关内容

  • 没有找到相关文章

最新更新