gpt4 book ai didi

C++ 指针赋值语句似乎不起作用/做任何事情

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:11:38 26 4
gpt4 key购买 nike

我在模板类(一个简单的 FIFO 队列)中有以下方法,在 GDB 调试时,我发现将指针“previous”重新分配给“current”的语句似乎什么也没做。执行此语句时,previous 开始为 NULL,current 不为 NULL,但 previous 仍为 NULL。有没有人见过这样的事情?

inline int search(QueueEntry<T> *current,QueueEntry<T> *previous, unsigned long long t)
{
while(current && !(current->getItem()->equals(t)))
{
previous = current; //**this line doesn't seem to work**
current = current->getNext();
}
if(current)
return 1;
return 0;
}

最佳答案

赋值被编译器优化掉了,因为它对函数的行为没有任何影响。每次循环都重新分配 previous,但不对它做任何其他事情。

错误(如果有的话)在函数的其余部分。

关于C++ 指针赋值语句似乎不起作用/做任何事情,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6751779/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com