C++ binary_function 中的分段错误
我正在使用 Visual Studio 2010 Beta 2(也尝试使用 NetBeans),并且在以下代码中遇到分段错误:
// One of the @link s20_3_3_comparisons comparison functors@endlink.
template <class _Tp>
struct less : public binary_function<_Tp, _Tp, bool>
{
bool
operator()(const _Tp& __x, const _Tp& __y) const
{ return __x < __y; } //this is the problem line
};
我不知道我的程序中的什么名称,但我正在尝试找出答案。 (我认为这是一张地图)有谁知道该怎么做,或者以前遇到过这种情况吗?
I'm using Visual Studio 2010 Beta 2 (also tried with NetBeans), and I'm having a segmentation fault in the following code:
// One of the @link s20_3_3_comparisons comparison functors@endlink.
template <class _Tp>
struct less : public binary_function<_Tp, _Tp, bool>
{
bool
operator()(const _Tp& __x, const _Tp& __y) const
{ return __x < __y; } //this is the problem line
};
I don't know what in my program calls it, but I am trying to find out. (I think it's a map) Does anyone know what to do, or has encountered this before?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这段代码没有任何问题;问题出在调用它的代码中。 (事实上,由于这是 STL 的一部分,因此该代码极不可能出现问题。)由于释放的内存、NULL 指针或类似的原因,它可能会传递一个无效的引用。
默认情况下,
less
用于 std::map、std::set,以及可能我没有想到的其他一些容器现在,您可以检查是否有任何容器,例如那些留下了无效值的容器。(实际上,最简单的方法是按照 James McNellis 所说的那样 - 在调试器中运行它并查看堆栈跟踪。)
There's nothing wrong with this code; the problem lies in your code that's calling it. (In fact, since this is part of the STL, it's extremely unlikely that there's a problem with this code.) It's probably getting passed an invalid reference due to deallocated memory, a NULL pointer, or similar.
less
is used, by default, for std::map, std::set, and likely some other containers that I'm not thinking of right now, so you can check if you have any containers such as those that are being left with invalid values.(Really, though, the easiest approach is to do as James McNellis said - run it in a debugger and look at your stack trace.)
我昨天也遇到了同样的问题。
这是经过尝试和测试的代码,因此导致崩溃的可能性非常低。
通常这种崩溃有三种可能性:
要诊断此问题,请在 VS 调试器中运行代码。当您的应用程序崩溃时,查看参数值并检查调试器中显示的堆栈跟踪是否与您应该看到的堆栈跟踪相同(单击堆栈跟踪中的每个条目并查看代码正在调用它应该调用的内容) )。
I had the same problem yesterday.
This is tried and tested code so there is a very low probability this is causing the crash.
Usually there are three possibilities for this crash:
To diagnose this, run your code in the VS debugger. When your application crashes, look at the parameter values and check that the stack trace shown in the debugger is the same as the stack trace you should see (click on each entry in the stack trace and look and see the code is calling what it should).