- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所有,在删除该节点之前实现对包含行号“x”的节点的搜索,我遇到了一个问题,其中正向和反向搜索都识别正确的节点,但调用者节点地址的指针报告不同反向搜索优于正向搜索?这仅适用于最后一个节点(最高行号)。如果仅使用前向搜索(pba_fwd_iter_test),则最后一个节点将被正确删除。但是,如果使用反向搜索(pba_rev_iter_test),则“(victim->next)->prev =victim->prev;”设置的地址不正确,它设置“(victim->next)->prev = (victim->next)->prev”。例如,通过反向搜索到达结束节点,然后执行delete_node 结果如下:
49: 7 - (line to delete) This is a line of text that is somewhere around 50 to 80 characters in length
48 - prev: 0x604a80 cur: 0x604b10 next: 0x604ba0
49 - prev: 0x604b10 cur: 0x604ba0 next: 0x603010 <-- delete_node
0 - prev: 0x604ba0 cur: 0x603010 next: 0x6030a0
48 - prev: 0x604a80 cur: 0x604b10 next: 0x603010
49 - prev: 0x604b10 cur: 0x604ba0 next: 0x603010 <-- (node deleted)
0 - prev: 0x603010 cur: 0x603010 next: 0x6030a0
\_______________\______ Error (should be prev: 0x604b10)
@WhosCraig 慷慨地帮助了 delete_node 函数,该函数工作正常,但我无法弄清楚为什么当使用反向搜索结果定位同一节点时,delete_node 无法设置“(victim->next)->prev =victim->上一页;”适本地。作为反向搜索的测试,我只是简单地向开头添加一个节点,然后向前一个节点返回到有问题的节点,然后 delete_node 工作正常。 (只需附加:list = &(*list)->prev; list = &(*list)->next;。因此,问题与通过反向搜索到达结束节点时的指针状态有关而不是正向搜索——这就是我需要帮助弄清楚的。这是正向和反向搜索以及快速 ->prev ->next 之后的指针地址的输出:
=========== pba_fwd_iter_test() ===========
passing list = &(*list)->next to tstpptr (0x605b28)
tstpptr(): list : 0x605b28
tstpptr(): &list : 0x7ffff14633a8
tstpptr(): *list : 0x605ba0
tstpptr(): &(*list) : 0x605b28 <- caller's address reported
tstpptr(): &(**list): 0x605ba0 with forward search
tstpptr(): &(*list)->next : 0x605bb8
=========== pba_rev_iter_test() ===========
passing list = &(*list)->next to tstpptr (0x604020)
tstpptr(): list : 0x604020
tstpptr(): &list : 0x7ffff14633a8
tstpptr(): *list : 0x605ba0
tstpptr(): &(*list) : 0x604020 <- caller's address reported
tstpptr(): &(**list): 0x605ba0 with reverse search
tstpptr(): &(*list)->next : 0x605bb8
passing list = &(*list)->next to tstpptr (0x605b28)
tstpptr(): list : 0x605b28
tstpptr(): &list : 0x7ffff14633a8
tstpptr(): *list : 0x605ba0
tstpptr(): &(*list) : 0x605b28 <- caller's address reported after
tstpptr(): &(**list): 0x605ba0 &(*list)->prev; &(*list)->next
tstpptr(): &(*list)->next : 0x605bb8
以下是相关的代码片段,开头带有完整源代码的链接。感谢您提供的任何帮助:
/*
full source: http://www.3111skyline.com/dl/dev/prg/src/ll-double-cir-1.c.txt
*/
struct record
{
char *line;
int lineno;
int linetype;
struct record *prev;
struct record *next;
};
typedef struct record rec;
void // traverse in fwd direction to find hightest line no.
pba_fwd_iter_test (rec **list, int num);
void // traverse in rev direction to find hightest line no.
pba_rev_iter_test (rec **list, int num);
void // dump the pointers for examination
tstpptr (rec **list);
int main (int argc, char *argv[]) {
// <snip> fill struct with 50 records for testing (lineno '0' based 0-49)
pba_fwd_iter_test (&textfile, 49);
pba_rev_iter_test (&textfile, 49);
return 0;
}
void
pba_fwd_iter_test (rec **list, int num) {
printf ("=========== %s() ===========\n",__func__);
int linemax = getmaxline (*list);
int iterno = 0;
while (((*list)->lineno != num) && (iterno <= linemax)) {
iterno++;
list = &(*list)->next;
}
printf ("passing list = &(*list)->next to tstpptr (%p)\n", list);
tstpptr (list);
}
void
pba_rev_iter_test (rec **list, int num) {
printf ("=========== %s() ===========\n",__func__);
int linemax = getmaxline (*list);
int iterno = 0;
while (((*list)->lineno != num) && (iterno <= linemax)) {
iterno++;
list = &(*list)->prev;
}
printf ("passing list = &(*list)->next to tstpptr (%p)\n", list);
tstpptr (list);
// increment prev then next and check ptr values again
list = &(*list)->prev;
list = &(*list)->next;
printf ("passing list = &(*list)->next to tstpptr (%p)\n", list);
tstpptr (list);
}
void
tstpptr (rec **list) {
fprintf (stdout, "%s(): list : %p\n", __func__, list);
fprintf (stdout, "%s(): &list : %p\n", __func__, &list);
fprintf (stdout, "%s(): *list : %p\n", __func__, *list);
fprintf (stdout, "%s(): &(*list) : %p\n", __func__, &(*list));
fprintf (stdout, "%s(): &(**list) : %p\n\n", __func__, &(**list));
fprintf (stdout, "%s(): &(*list)->next : %p\n\n", __func__, &(*list)->next);
}
最佳答案
我想我看到了问题所在 - 我认为不存在这个问题。重要的值是*list
,它在所有情况下都是相同的。我认为打印列表和 &list 等只会使问题变得模糊。
在前向迭代器中,list
指向项目 #48 的 next
变量的位置。
在向后迭代器中,list
指向项目 #0 的 prev
变量的位置。
在这两种情况下,*list 都指向正确的项目 #49。
如果这两个函数只采用 rec *
而不是 rec **
,那么它们会更简单,那么采用list
变量不是您想要的。
关于C循环双链表: traverses fwd/rev for end node gives different pointer address,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22623189/
在指向指针的指针上使用指针算术是否定义明确? 例如 int a=some_value; int* p=&a; int**p2=&p; 现在对 p2 执行算术是否是定义明确的行为?(例如 p2+1、p2
我正在尝试使用一个函数来替代 C 中的 scanf()。该函数是由第三方编写的,并进行了相应的定义: ScanDecimal16uNumber - Scans a decimal 16bit unsi
我正在尝试为 Sundials CVODE 编写 CFFI 包装器图书馆。 SWIG 被 Sundial header 阻塞,因为它们相互关联,并且 SWIG 找不到合适的 header ,所以我手工
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: pass by reference not working 我正在阅读一些教程 linklistproblem在互联
我有一个代码片段很难理解。 char *c; // c is uni dimensional table ( single row ) char **p ; // p is a two dimen
我正在将一些代码移植到 Windows 并且被难住了。有一些代码在启动时自动运行以将指针复制到指针,并在退出时再次运行以删除指向指针的指针(如果它不为空)。 我已经创建了一个示例程序来重现该行为 in
将非 const 指针转换为 const 指针是合法的。 那为什么将指向非const的指针转换为指向const的指针是不合法的呢? 例如,为什么下面的代码是非法的: char *s1 = 0; con
将非 const 指针转换为 const 指针是合法的。 那为什么将指向非const的指针转换为指向const的指针是不合法的呢? 例如,为什么下面的代码是非法的: char *s1 = 0; con
将指向非常量的指针转换为指向常数的指针是合法的。 那么为什么将指向非const的指针转换为指向const的指针是不合法的呢? 例如,为什么下面的代码是非法的: char *s1 = 0; const
之间有什么区别 procedure(some_routine), pointer :: ptr ptr => null() 和 procedure(some_routine), pointer ::
只是为了消除一些困惑。我最近遇到了这段代码(使用指针到指针): int encode(unsigned char type, uint64_t input_length, unsigned char*
我已经阅读了我能找到的有关 C/C++ 指针的内容,但其中大部分是介绍性的,虽然它可以帮助您理解它们的使用,但在许多情况下,现有代码会抛出难以破译的示例。 我确实看到了一些例子,他们将一行代码分解成它
我一直在关注的学习数据结构的书使用“单指针”作为函数中的参数,这些函数在链表的不同位置添加新节点,例如在开始,在结束。同样在删除的情况下使用“pointer-to-pointer”。在所有这些情况下,
考虑这段代码: #define MAX 4 ............ ............ int** ptr = (int**)malloc(sizeof(int*)*MAX); *ptr =
如何将指向 void 对象的指针转换为类对象? 最佳答案 使用 static_cast。请注意,只有当指针确实指向指定类型的对象时,您才必须这样做;也就是说,指向 void 的指针的值取自指向此类对象
我假设一种语言的实现允许您将指针视为整数,包括对它们进行标准算术。如果由于硬件限制这是不现实的,请告诉我。如果编程语言通常没有这么强大的指针运算,但是在实践中是可行的,那么我仍然想知道这种实现BigI
我是一名 nodejs 开发人员,我通常为我的应用程序使用一个结构,该结构包含一个配置包/对象,该对象包含对我常用的库和配置选项的引用。通常,此配置对象也包含我的数据库连接,并且可以通过我的应用程序访
我已经在几个上下文中阅读过“胖指针”这个术语,但我不确定它的确切含义以及它何时在 Rust 中使用。指针似乎是普通指针的两倍,但我不明白为什么。它似乎也与特征对象有关。 最佳答案 术语“胖指针”用于指
这是让我困惑的代码。 static char *s[] = {"black", "white", "pink", "violet"}; char **ptr[] = {s+3, s+2, s+1, s
通用指针允许您创建指向指针的指针: void foo(Object **o) {} int main() { Object * o = new Object(); foo(&o); } s
我是一名优秀的程序员,十分优秀!