C++ binary_function 中的分段错误

发布于 2024-09-06 06:27:21 字数 470 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

毅然前行 2024-09-13 06:27:21

这段代码没有任何问题;问题出在调用它的代码中。 (事实上​​,由于这是 STL 的一部分,因此该代码极不可能出现问题。)由于释放的内存、NULL 指针或类似的原因,它可能会传递一个无效的引用。

默认情况下,less 用于 std::mapstd::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.)

戴着白色围巾的女孩 2024-09-13 06:27:21

我昨天也遇到了同样的问题。

这是经过尝试和测试的代码,因此导致崩溃的可能性非常低。

通常这种崩溃有三种可能性:

  • 另一个问题是导致调用此函数的数据损坏
  • 另一个问题是导致堆栈损坏导致在不应该是
  • 两者的任何组合 时调用此函数上面的可能性。

要诊断此问题,请在 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:

  • another problem is causing a corruption of the data that this function is called on
  • another problem is causing a corruption of your stack causing this function to be called when it shouldn't be
  • any combination of the two possibilities above.

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文