- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个通用的双向链表实现,并且正在研究插入。插入后,我尝试从 footer 开始向后遍历列表,但出现总线错误。为了测试代码并尝试隔离错误,我在其中添加了打印语句(不是最好的调试过程,但有助于让我一目了然地告诉我我的代码在做什么)。为了尝试查看问题出在哪里,每次插入后我都会询问列表中倒数第二个元素的值。我总是按 5、2、10、80、4、1、7、8 的顺序插入元素,在将 4 插入列表后,它一直卡住。程序的完整代码如下。
dlist_t *
insert_in_order (dlist_t *list, void *value, size_t size,
int (*cmp_fptr)(const void *, const void *))
{
dlnode_t * prev = NULL;
dlnode_t * current = list->head;
dlnode_t * newnode = safe_malloc(sizeof(dlnode_t));
newnode->data = value;
newnode->next = NULL;
newnode->prev = NULL;
printf("Beginning list loop for %d.\n", *(int *) newnode->data);
while (current != NULL && cmp_fptr(newnode->data, current->data) != -1)
{
prev = current;
current = current->next;
}
printf("Insertion point found.\n");
newnode->next = current;
newnode->prev = prev;
if (prev == NULL)
{
printf("Setting for new head\n");
list->head = newnode;
}
else
{
printf("Setting previous next to new node\n");
prev->next = newnode;
}
if (current == NULL)
{
printf("setting for new foot.");
list->foot = newnode;
}
else
{
printf("Setting for new current previous\n");
current->prev = newnode;
}
list->list_len++;
list->size = sizeof(list);
printf("Insertion compete for %d\n\n", *(int *) newnode->data);
printf("Data for surrounding:\n");
if(newnode->next !=NULL)
{
printf("Next is %d \n", *(int *) newnode->next->data);
}
if(newnode->prev != NULL)
{
printf("Prev is %d \n\n", *(int *) newnode->prev->data);
}
if(list->foot->prev != NULL)
{
printf("Gonna print secondlast!\n");
printf("secondlast is%d \n\n", *(int *)list->foot->prev->data);
}
return list;
}
列表定义很基础,就
struct dlnode
{
void *data; /* A pointer to a generic satellite data payload */
dlnode_t *next; /* A pointer to the next item in the list */
dlnode_t *prev; /* A pointer to the previous item in the list */
};
typedef struct
{
dlnode_t *head; /* A pointer to the head node of the list */
dlnode_t *foot; /* A pointer to the foot node of the list */
int list_len; /* Total number of items in the list */
int size; /* Size of the list in bytes */
} dlist_t;
您可以根据需要更改函数定义,而 safe_malloc 只是 malloc 的一种快捷方法,如果您自己测试代码,可以替换它。 cmp_fptr 是指向简单“大于 b”方法的函数指针。
编辑:更新线路
printf("secondlast is%d \n\n", *(int *)list->foot->prev->data);
是导致程序停止的原因,我使用了调试器。将项目插入列表时,它会在插入几次后停在该行。以下是我现在正在使用的测试工具代码。
int *
alloc_data (int val)
{
int *rv = safe_malloc (sizeof (int));
*rv = val;
return (rv);
}
int
main (void)
{
dlist_t *list = NULL;
int *num = NULL, *rv = NULL;
dlnode_t *tnode = NULL;
list = make_empty_list ();
list = insert_in_order (list, alloc_data (5), sizeof(int), cmp_int);
list = insert_in_order (list, alloc_data (2), sizeof(int), cmp_int);
list = insert_in_order (list, alloc_data (10), sizeof(int), cmp_int);
list = insert_in_order (list, alloc_data (80), sizeof(int), cmp_int);
list = insert_in_order (list, alloc_data (4), sizeof(int), cmp_int);
list = insert_in_order (list, alloc_data (1), sizeof(int), cmp_int);
list = insert_in_order (list, alloc_data (7), sizeof(int), cmp_int);
list = insert_in_order (list, alloc_data (8), sizeof(int), cmp_int);
return(EXIT_SUCCESS);
}
还有更多内容,但我目前正在测试的就是这些。
感谢 list->size 提示,我不太确定我最初是怎么想的。
edit2:感谢 safe_malloc 错误发现,我认为这是问题的原因,但我仍然遇到同样的错误。调试器在插入 4 后给了我一个 sigsegv(段错误),它到达了我要求 list->foot->prev->data(见上文)的理智行。
最终编辑:通过为节点数据正确分配足够的空间来解决问题。感谢那些提供帮助的人。我的代码中还有其他问题,但这最适合另一个问题,并且关于不同的代码片段。
最佳答案
一些事情:
正如已经说过的那样, list->size = sizeof(list);
可能不会按照您的想法行事
作为参数传递的 size_t size
可能是您的主要问题并且看起来很危险(调用函数时这个变量值是多少?)
使用错误的大小执行 dlnode_t * newnode = safe_malloc(size);
可以解释您的问题
dlnode_t * newnode = safe_malloc(size);
应该替换为
dlnode_t * newnode = safe_malloc(sizeof(dlnode_t));
最后,在您的列表中,您直接使用 void *value
而不是它的副本。所以如果你不总是在同一个 block 中调用这个函数,你就会遇到问题
使用相同的函数签名,我认为 size 参数应该代表 value 参数的大小,以便制作它的 malloc/memset 并将其保存在您的列表中
关于c - 排序的双向链表在反向访问时给出总线错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7027642/
我有 2 个类:User 和 UserPicture,它们具有 1:1 关系。 public class User { @Id @GeneratedValue(strategy=G
使用ssh转发时,我无法针对远程服务器使用cvs和ftp进行提交。是否可以让服务器对我的机器发起请求-我希望服务器上的Web应用程序调用我的机器上的REST方法。 谢谢。 尼古拉·G。 最佳答案 是的
我正在 Python 2.7.12 中实现双向 A* 算法,并在 Russell 和 Norvig 第 3 章的罗马尼亚 map 上进行测试。边具有权重,目的是找到两个节点之间的最短路径。 这是测试图
您能否建议一种映射或类似的数据结构,让我们可以轻松地相互获取值和键。也就是说,每个都可以用来寻找另一个。 最佳答案 Java 在其标准库中没有双向映射。 例如使用 BiMap 来自Google Gua
我想同步两个数据库运行时 服务器 A:安装了公共(public) IP 和 mysql 的 Amazon ec2。服务器B:这是局域网中带有mysql的私有(private)机器。 (IP是私有(pr
保存双向@OneToOne 映射时,hibernate 是否应该在两个表上都记录? 我有一个包含 applicant_id 列的表 interview,它引用了包含字段 interview_id 的
我喜欢新的 SwipeRefreshLayout!它看起来很棒,而且非常容易使用。但我想在两个方向上使用它。我有一个消息屏幕,我想通过从上到下滑动来加载旧消息,我想通过从下到上滑动来加载新消息。 这个
使用 ICS 4.0.1(愿意升级到 4.0.3)(不会 root 和重写 android 操作系统) 在接收到 android beam 后,是否可以将 NDEF 消息发送回 android 手机
我想知道处理这种 git 场景的最佳方法: Git 仓库:CoreProduct Git repo b: SpecificCustomerProduct 是从 a fork 出来的 到目前为止,我们一
这个问题在这里已经有了答案: How to implement an efficient bidirectional hash table? (8 个回答) 关闭2年前。 我在 python 中做这个
您能否推荐一种 map 或类似的数据结构,我们可以在其中轻松地从彼此获取值和键。也就是说,每个都可以用来寻找另一个。 最佳答案 Java 在其标准库中没有双向映射。 例如使用 BiMap 来自 Goo
Java中是否有类似双面列表的东西?也许第三方实现? 这里有一个小例子来证明我的想法。 原始状态: 答:0-1-2-3 | | | | 乙:0-1-2-3 删除 B 中的元素 1 后: 空值 | 答:
我有两个实体通过这样的双向 OneToOne 关联连接: @Entity class Parent { @NotNull String businessKey; @OneToO
我已将 Vagrant 配置为使用 Rsync 共享文件夹而不是(非常慢)vboxsf VirtualBox 默认提供的文件系统: Vagrant.configure("2") do |config|
@keyframes mgm { from { max-height: 250px; } to { max-height: 0px; } } .mgm {
我想了解有关使用双向 LSTM 进行序列分类时合并模式的更多详细信息,尤其是对于我还不清楚的“Concat”合并模式。 根据我对这个方案的理解: 在将前向和后向层的合并结果传递到 sigmoid 函数
我有兴趣将本地 git 存储库设置为远程存储库的镜像。我已经阅读了一些可能相关的帖子,但主要区别在于我需要对两个存储库进行读写访问。 大多数时候,用户会针对 Repo A 工作,但是有时他们会针对 R
我已经仔细阅读了文档 https://firebase.google.com/docs/database/web/read-and-write以及网上很多例子。但这里有一个脱节:在将对象添加到数据库时
这个问题已经有答案了: Hibernate bidirectional @ManyToOne, updating the not owning side not working (3 个回答) 已关闭
我知道有很多关于它的问题,但我找不到针对我的问题的好的答案。 我使用 Jboss 作为 7,Spring 和 Hibernate (4) 作为 JPA 2.0 提供程序,因此我有简单的 @OneToM
我是一名优秀的程序员,十分优秀!