[英]C Circular Doubly Linked List with static head initialization of next/prev pointers to static head address
I want to initialize a Circular Doubly Linked List next/previous pointers to point at the address of a statically allocated element used as sentinel/first element.我想初始化一个循环双向链表的 next/previous 指针以指向用作哨兵/第一个元素的静态分配元素的地址。 In this way, I can simplify the list operations.
这样,我可以简化列表操作。 I tried the following code:
我尝试了以下代码:
struct ErrorDataElement {
struct ErrorDataElement *next;
struct ErrorDataElement *prev;
enum ErrorType type;
int code;
char **messages;
char *file;
char *function;
int line;
} _errorList = { &_errorList, &_errorList, 0, -1, NULL, NULL, NULL, 0 };
When I print &_errorList
memory address (in a sub-function called from main() function) I get a different address from _errorList.next
and _errorList.prev
.当我打印
&_errorList
内存地址(在从 main() 函数调用的子函数中)时,我从_errorList.next
和_errorList.prev
得到不同的地址。
_errorList.next
and _errorList.prev
have coherently the same value. _errorList.next
和_errorList.prev
具有一致的相同值。
If I inspect using the following code:如果我使用以下代码进行检查:
printf("%d %d", _errorList.code, _errorList.next->code);
I see that _errorList.code = -1
as expected, while _errorList.next->code = 1001
, indicating to me that the value of _errorList.next
has been initialized to some incorrect memory address.我看到
_errorList.code = -1
符合预期,而_errorList.next->code = 1001
向我表明_errorList.next
的值已初始化为某些不正确的内存地址。 I suspect this is due to relative/absolute memory addressing during function initialization, but I can't figure out exactly and how to correctly initialize the static element, or whether or not is even possible such initialization.我怀疑这是由于函数初始化期间的相对/绝对内存寻址,但我无法确切地弄清楚以及如何正确初始化静态元素,或者甚至是否可能进行这种初始化。
I cannot reproduce your issue.我无法重现您的问题。 When I execute this, it prints the expected result :
当我执行此操作时,它会打印预期结果:
void afunc(struct ErrorDataElement *elem) {
printf("%p %p %p\n", elem, elem->next, elem->prev);
printf("%d %d %d\n", elem->code, elem->next->code, elem->prev->code);
}
int main() {
afunc(&_errorList); // prints ==> 0x601030 0x601030 0x601030
// -1 -1 -1
return 0;
}
The same is true if I print directly from main and I cannot find a case where what you explain happens.如果我直接从 main 打印并且找不到您解释的情况,情况也是如此。 Cannot you show your exact codes ?
你不能显示你的确切代码吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.