在下面的代码中,我不知道为什么我从不输入if。对我来说,这是有道理的,它进入了如果:
void Mem_Coalesce(){
list_t* temp;
temp = freep;
if(free_node_count == 2){
//will finish
printf("Addr of temp is %llx\n\n", (long long unsigned) temp);
printf("Size of Temp is %d\n\n", temp->size);
printf("Addr of end of temp free node space is %llx\n\n", (long long unsigned) ((char*)temp + temp->size));
printf("Addr of start temp->next is %llx\n\n", (long long unsigned) temp->next);
if( ((char*)temp + (temp->size)) == (char*)(temp->next)){
printf("Entered the if statement\n");
temp->size += (temp->next)->size;
temp->next = NULL;
}
}else {
while(temp->next != NULL ){
if( ((char*)temp + (temp->size)) == (char*)(temp->next)){
temp->size += (temp->next)->size;
printf("coalesced size is %d temp->size \n", temp->size);
temp->next = (temp->next)->next;
((temp->next)->next)->prev = temp;
}
temp=temp->next;
}
}
}
结果是
Addr of temp is 7f9c1e89b070
Size of Temp is 200
Addr of end of temp free node space is 7f9c1e89b138
Addr of start temp->next is 7f9c1e89b278
正如你所见,它从未进入
if statement
。另外,让我知道是否有其他算法可以更有效地进行内存合并。 最佳答案
您在此处以十进制打印temp->size
:
printf("Size of Temp is %d\n\n", temp->size);
^^
打印十六进制指针值时:
printf("Addr of temp is %llx\n\n", (long long unsigned) temp);
^^^^
十进制是十六进制的
200
和C8
+0xC8
=0x70
。因此,当您将0x138
添加到0xC8
时,结果是0x7f9c1e89b070
,它不等于0x7f9c1e89b138
,因此您不输入if语句。