以下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 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)
请注意,我在第47行处有一个断点,因此应该执行第46行。但正如清楚地表明的那样,这项任务并没有真正成功。
我根本不明白为什么。
修改 的 stack.cpp
#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;
}
stack.hpp
#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_
EDIT2: 将第46行修改为
*head_element=*(head->element);
解决了我的计划,但我仍然对wtf正在进行中感到好奇。
答案 0 :(得分:4)
head_element=head->element;
由编译器优化,因为它不会导致可观察行为。
您可以设置局部变量head_element
的值,但在该函数结束时销毁之前,该变量的值永远不会被使用。
通过值传递的函数参数与局部变量相同。