C++ 已跳过int*的平凡赋值
以下gdb摘录应说明这一点:C++ 已跳过int*的平凡赋值,c++,pointers,C++,Pointers,以下gdb摘录应说明这一点: Breakpoint 1, stack<int>::top (this=0x603010, head_element=0x7fffffffe560) at stack.cpp:47 47 return 1; (gdb) list 46 41 { 42 if(head==0) 43 return 0; 44 else 45 { 46
Breakpoint 1, stack<int>::top (this=0x603010, head_element=0x7fffffffe560)
at stack.cpp:47
47 return 1;
(gdb) list 46
41 {
42 if(head==0)
43 return 0;
44 else
45 {
46 head_element=head->element;
47 return 1;
48 }
49 }
50
(gdb) p head_element
$1 = (int *) 0x7fffffffe560
(gdb) p head->element
$2 = (int *) 0x603050
(gdb)
解决了我的程序,但我仍然对wtf正在进行的工作感到好奇。
head\u element=head->element代码>已由编译器优化,因为它不会导致可观察的行为
设置局部变量head\u元素的值
,但在函数结束时销毁该变量之前,该变量的值永远不会被使用
通过值传递的函数参数与局部变量的类型相同 你能把head
和head\u元素的声明张贴出来吗?@lc。请参见编辑。加满code@Cheeku:Hmmhead\u element=head->element
无效,因为您所做的只是更改一个局部变量head\u element
,并且该变量在该赋值之后永远不会使用。这就像执行voidfoo(inti){i=14;}
:该赋值不可能影响程序。但是,*head\u element=*(head->element)
确实有效果,因此它没有被优化。如果你想在没有这种奇怪事情的情况下进行调试,那么请确保优化级别为0,正如@gmbeard所说的那样。@Cheeku@Claudiu已经给了你答案;您正在按值传递head\u元素
。当然,它是一个指针,但当函数returns@CraigYoung对函数返回后,将不会保留对head\u元素的任何赋值@克劳迪乌,也许你应该写一个完整的答案来解释。此评论线程已经太长了,如何避免-O0帮不了你,你不能。它是C++语言定义的一部分。只要可执行文件产生正确的输出,编译器就可以使用它喜欢的任何汇编指令。您可以尝试使head_元素不稳定,但不能保证它会做任何事情。@M.M我还忘了提到,对于我正在使用的特定测试用例,该行在调用前2次时确实执行,但第三次失败。
#include "stack.hpp"
template <class T>
stack<T>::stack()
{
//empty stack
head=0;
}
template <class T>
stack<T>::stack(const stack<T> &to_copy_to)
{
head = to_copy_to.head;
}
template <class T>
void stack<T>::operator=(const stack<T> &rhs)
{
head = rhs.head;
}
template <class T>
stack<T>::~stack()
{
delete head;
}
template <class T>
void stack<T>::push(T n)
{
struct stack_node *new_node = new struct stack_node;
new_node->element = new T;
*(new_node->element) = n;
new_node->next=head;
head = new_node;
}
template <class T>
int stack<T>::top(T* head_element)
{
if(head==0)
return 0;
else
{
*head_element=*(head->element);
return 1;
}
}
template <class T>
void stack<T>::pop()
{
if(head!=0)
{
head=head->next;
}
}
template <class T>
int stack<T>::size()
{
int count = 0;
stack_node* iter=head;
while(iter!=0)
{
count++;
iter = iter->next;
}
return count;
}
#ifndef _STACK_HPP_
#define _STACK_HPP_
template<class T>
class stack {
private:
// Add your member variables here
//I can't work around without a node structure
struct stack_node{
T* element;
stack_node* next;
}*head;
public:
/**
* Default constructor for the stack class
*/
stack();
/**
* Copy constructor for the stack class.
* Params:
* const stack &to_copy_to : A reference to the stack object to be copied
* into.
*/
stack(const stack &to_copy_to);
/*
* Assignment overload, to fulfill rule of three
*/
void operator=(const stack &rhs);
/**
* Default destructor for the stack class
*/
~stack();
/**
* Pushes an object of type T on the top of the stack
* Params:
* T n : The object to be pushed on the top of the stack
*/
void push(T n);
/**
* Gives the element on the top of the stack, if any
* Params:
* T *top_element : Pointer to the location where the top element is to be
* stored before returning
* Return value:
* int : Positive if stack is non empty, negative if it is empty
*/
int top(T *top_element);
/**
* Removes the element on the top of the stack, if any
*/
void pop();
/**
* Returns the number of elements in the stack
* Return value:
* int : Number of elements in the stack
*/
int size();
};
#endif //_STACK_HPP_
*head_element=*(head->element);