总线错误(核心转储) [英] Bus Error(core dumped)

查看:908
本文介绍了总线错误(核心转储)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Stack模板。堆栈被声明为数组。

[

模板< typename T,size_t N> ;; // N是100

类TStack

{

...

私人:

T data_ [N];

...

}


我在这个类模板中有几个函数, Pop()//删除堆栈的最后一项,Push()//将项目推送到堆栈,Size()//返回堆栈的大小,Capacity()//返回N


我有一个cpp程序,使用菜单界面测试我的模板的功能。当我选择Pop()而不将任何东西推到堆栈时,我得到一个总线错误(核心转储)。


[

模板< typename T,size_t N>

... :: Pop()

{

if(size_ == 0){std :: cerr<< Stack is empty''\''" ;;}

else {return data_ [size_-1];} // size_初始化为0并在项目被推送到0时递增弹出时堆叠并递减

}

]


当我将一个项目推送到堆栈然后选择Pop(),它做了预期的事情。


我应该怎么做data_以确保不会发生这种情况。我知道总线错误是一个分段错误,它发生是因为我试图访问一个未分配内存的值,但我不知道如何在这种情况下修复它。在指南中,我不允许使用operator new或delete来确保分配。


提前谢谢。

I am writing a Stack template. The stack is declared as an array.
[
template <typename T, size_t N>; // N is 100
class TStack
{
...
private:
T data_[N];
...
}

I have several functions in this class template, Pop() //removes last item of stack, Push() //pushes an item to the stack, Size() //returns the size of the stack, Capacity()// returns N

I have a cpp program that tests the functionality of my template using a menu interface. When I choose Pop() without pushing anything to the stack, I get a Bus Error(core dumped).

[
template <typename T, size_t N>
... ::Pop()
{
if (size_ == 0) {std::cerr << "Stack is empty''\n''";}
else {return data_[size_-1];} //size_ is initialized to 0 and incremented when item are pushed to the stack and decremented when they are popped
}
]

When I Push() an item to the stack and then choose Pop(), it does what is expected.

What should I do to data_ to ensure that this does not happen. I understand that the Bus Error is a segmentation fault and it happens because I am trying to acces a value that is not allocated memory, but am not sure how to fix it in this situation. In the guidelines I am not allowed to use operator new or delete to insure allocation.

Thanks in advance.

推荐答案

堆栈<>的错误是什么? C ++标准模板库中的模板?


这是某种学校作业吗?
What''s wrong woith the stack<> template in the C++ Standard Template Library?

Is this some sort of school assignment?


weaknessforcats,是的。
weaknessforcats, yes it is.


您正在为堆栈使用固定数组。不好。您应该使用链接列表,以便在列表中添加或删除项目时添加/删除节点。


在这种情况下,data_是T *。


当data_为0时,堆栈为空。

链表将避免seg错误,因为你不需要各种各样的数组管理代码这个班级。


您只需要一个链接列表,您可以在列表末尾添加或删除。


添加是你的Push(),Remove()就是你的Pop()。


应该有一个Top()来查看堆栈的顶部(链表中的最后一个元素)删除它。
You are using a fixed array for your stack. Not good. You should be using a linked list so you add/remove nodes as items are added or removed from the list.

In this case data_ is a T*.

When data_ is 0, the stack is empty.

A linked list will avoid the seg fault since you won''t need all sorts of array management code in the class.

All you need is a linked list where you add to or remove from the end of the list.

The add is your Push(), the Remove()is your Pop().

There should be a Top() to view the top of the stack (last element in the linked list) without removing it.


这篇关于总线错误(核心转储)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆