我试图在struct node_delay_info
的另一个链接列表的每个节点中实现struct node
的链接列表。
我在两个函数中扫描列表:callback_function()
分配和填充列表,study_list()
打印struct node_delay_info
的值。
当我使用callback_function()
成功填写所有字段时,study_list()
中列表的另一次扫描不会打印我在上一个函数中插入的值。
以下是代码:
typedef struct node_delay_info {
double delay_info;
struct node_delay_info *next;
} node_delay_info;
typedef struct node {
/* bunch of other data */
...
/* useful when reading values from the head list */
struct node_delay_info *first_elem;
/* useful when inserting new nodes at bottom list */
struct node_delay_info *last_elem;
struct node *next;
} node;
node *head = NULL;
/* malloc return values are not checked here for brevity */
void callback_function(/* args */) {
node *tmp = NULL;
if (head == NULL) { // if list is empty
node *new_node = (node *)malloc(sizeof(node));
node_delay_info *delay_node = (node_delay_info *)malloc(sizeof(node_delay_info));
new_node->next = NULL;
delay_node->delay_info = 0.0;
delay_node->next = NULL;
new_node->first_elem = delay_node;
new_node->last_elem = delay_node;
head = new_node;
} else { // if list is not empty
tmp = head;
while (tmp->next != NULL) {
if (/*the node of the list has some matching values for me*/) {
/* calculating stuff */
unsigned long delay = ...;
/* updating the delay_field value */
node_delay_info *d_info = (node_delay_info *)malloc(sizeof(node_delay_info));
d_info->delay_info = (double)delay;
d_info->next = NULL;
tmp->last_elem->next = d_info;
/* I checked the value of delay_info field inside d_info struct
and it is successfully filled with delay */
return; // I no longer need to search
} else {
/* the node of the list has no matching value, must go forward */
tmp = tmp->next;
}
}
/* if we are here we are at the end of the list and no element
of it matches my values, so I allocate a new node */
node *new_node = (node *)malloc(sizeof(node));
new_node->next = NULL;
node_delay_info *delay_node = (node_delay_info *)malloc(sizeof(node_delay_info));
delay_node->delay_info = 0.0;
delay_node->next = NULL;
new_node->first_elem = delay_node;
new_node->last_elem = delay_node;
tmp->next = new_node;
}
}
void study_list() {
node *temp = head;
node_delay_info *info_temp2;
while (temp->next != NULL) {
info_temp2 = temp->first_elem;
while (info_temp2->next != NULL) {
printf("%lf -> ", info_temp2->delay_info);
info_temp2 = info_temp2->next;
}
printf("\n");
temp = temp->next;
}
}
int main() {
...
/* variables and structs for libpcap */
pcap_loop(descr, how_many_pkts, my_callback, NULL);
study_list();
}
输出:
0.000000 - >
0.000000 - >
0.000000 - >
...
编辑1 :正在运行~$sudo valgrind --tool=memcheck --leak-check=full --show-reachable=yes ./main
会显示0.000000 ->
的输出,然后会让我想起x bytes in y blocks are still reachable in loss record
:我猜它指的是分配了各种{的内存{1}}我没有自由,但我不认为这是问题所在。
答案 0 :(得分:0)
在将新元素附加到列表末尾之后,原始代码缺少一行来更新尾指针tmp->last_elem
。请参阅下面标有// <-- This line was missing!
的行:
tmp = head;
while (tmp->next != NULL) {
if (/*the node of the list has some matching values for me*/) {
/* calculating stuff */
unsigned long delay = ...;
/* updating the delay_field value */
node_delay_info *d_info = (node_delay_info *)malloc(sizeof(node_delay_info));
d_info->delay_info = (double)delay;
d_info->next = NULL;
tmp->last_elem->next = d_info;
tmp->last_elem = d_info; // <-- This line was missing!
/* I checked the value of delay_info field inside d_info struct
and it is successfully filled with delay */
return; // I no longer need to search
} else {
/* the node of the list has no matching value, must go forward */
tmp = tmp->next;
}
}