简体   繁体   English

Visual Studio到gcc,代码在gcc中编译良好,但在运行时给出std :: badalloc错误

[英]Visual studio to gcc, code compiles well in gcc but gives std::badalloc error at runtime

I have a big code that uses only standard C++ libs and compiles well in gcc. 我有一个很大的代码,仅使用标准C ++库,并且可以在gcc中很好地进行编译。 As the code was actually written in VS C++ 6.0. 由于代码实际上是用VS C ++ 6.0编写的。 The code runs fine in visual studio but when I use gcc compiler it gives no errors on compilation and when I run it it gives this error "terminate called after throwing an exception at instance std::bad_alloc what() bad alloc" One more confusion is that it is a numerical simulation code, it doesn't show any exception while using gdb debugging and terminates successfully but doesn't show the right results. 该代码在Visual Studio中可以正常运行,但是当我使用gcc编译器时,编译时不会出现任何错误,当我运行它时,它会出现此错误“在实例std :: bad_alloc what()错误的alloc抛出异常后终止调用”另一个困惑它是一个数值模拟代码,在使用gdb调试时不会显示任何异常,并且可以成功终止,但是显示的结果不正确。 But using gdb it doesn't terminates anywhere. 但是使用gdb不会在任何地方终止。 So thats where I am stuck. 那就是我被困住的地方。 I am unable to diagnose where does the bad_alloc is actually happening. 我无法诊断bad_alloc实际上在哪里发生。

  1. The code consists of C and C++ routines 该代码由C和C ++例程组成
  2. Memory is allocated through new 内存是通过新分配的
  3. gdb doesn't show any segabort or any exception during debugging gdb在调试过程中不显示任何segabort或任何异常

How can I debug this problem? 如何调试此问题?

如果您使用的是Linux,请尝试尝试valgrind进行调试。

VC++ 6.0 is not exactly standards compliant. VC ++ 6.0并非完全符合标准。 The upgrade of the compiler (to a newer version) will catch previously unseen errors. 编译器的升级(到较新版本)将捕获以前看不见的错误。

There are three things you can do: 您可以做三件事:

  • Get are core dump from the unix version (I'm assuming you're using using gcc on a variant of unix), and then use gdb in a post-mortem manner to see where exactly the exception is triggered. 从unix版本获取核心转储(我假设您在unix的变体上使用gcc),然后以事后方式使用gdb来查看异常在何处触发。

  • Use a more recent version of Visual C++ (For example free Express 2010 edition) to check the software 使用最新版本的Visual C ++(例如免费的Express 2010版本)检查软件

  • Rewrite your allocations to be more robust. 重写您的分配,使其更可靠。 ie: check for null values. 即:检查空值。 Also you might override the new operator to do this globally. 另外,您可以覆盖新的运算符以全局进行此操作。

An exception object, even std::bad_alloc , is still an object and needs to be constructed before it can be thrown. 即使是std::bad_alloc异常对象,仍然是一个对象,需要对其进行构造才能引发它。 Hence, a breakpoint on std::bad_alloc::bad_alloc will break execution before it's thrown. 因此, std::bad_alloc::bad_alloc上的断点将引发之前中断执行。

Exceptions are meant to identify runtime errors, so the fact that the code compiles and then throws an exception at runtime is perfectly reasonable. 异常是用来标识运行时错误的,因此代码在运行时编译然后抛出异常这一事实是完全合理的。 std::bad_alloc means that you're running out of memory. std::bad_alloc表示您的内存不足。

However, tracking down why you're running out of memory is the hard thing. 但是,很难找到内存不足的原因。 I recently worked on some code that hadn't been updated since Visual C++ 6 and it turns out had a serious bug due to Visual C++ 6's nonstandard treatment of code. 最近,我处理了自Visual C ++ 6起就没有更新过的某些代码,由于Visual C ++ 6对代码的非标准处理,事实证明存在严重的错误。 The problem was basically: 问题基本上是:

long foo(int i)
{
    return foo(long(i));
}

long foo(long& l)
{
   return l * 100;
}

This is syntactically correct code. 这是语法正确的代码。 However, Visual C++ 6 does something wrong here: it interprets the call foo(long(i)) as calling long foo(long& l) . 但是,Visual C ++ 6在这里做错了什么:它将调用foo(long(i))为调用long foo(long& l) This is incorrect. 这是不正确的。 The expression long(i) creates a temporary object and temporary objects cannot bind to non-const references. long(i)表达式创建一个临时对象,并且临时对象不能绑定到非const引用。 So, according to C++ standard method overload resolution, this simply calls long foo(int i) again and again recursively. 因此,根据C ++标准方法重载解析,此操作简单地一次又一次地调用long foo(int i) In my case, the infinite recursion caused a stack overflow. 就我而言,无限递归导致堆栈溢出。 To fix this problem, long foo(long& l) needs to be either long foo(long l) or long(const foo& l) . 要解决此问题, long foo(long& l)必须是long foo(long l)long(const foo& l) I suspect you will find a similar problem in your code, but due to memory allocations you're getting a memory overflow before you get a stack overflow. 我怀疑您会在代码中找到类似的问题,但是由于内存分配,在出现堆栈溢出之前,您会遇到内存溢出的问题。

I fixed my problem by running Google's cpplint.py and looked for errors regarding non-cosnt references. 我通过运行Google的cpplint.py解决了我的问题,并查找了有关非粗略引用的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 代码在Sun Studio上编译但在gcc上出错 - code compiles on Sun Studio but gives error on gcc 运算符重载在Visual Studio 2013中编译,但不在gcc中编译 - Operator overloading compiles in Visual Studio 2013 but not gcc 代码在ideone中编译,但不在gcc中编译 - Code compiles in ideone but not with gcc 代码用clang编译但不用gcc编译 - Code compiles with clang but not with gcc 用 gcc 但不能用 g++ 编译的代码 - Code that compiles with gcc but not with g++ STL列表在Visual Studio 2013中编译的代码在Visual Studio 2019中给出了错误。想知道原因 - STL list Code that compiles in Visual Studio 2013 gives error in Visual Studio 2019. Wanted to know the reason 用于初始化std :: vector的初始值设定项列表 <std::function<bool(std::string)> &gt;使用g ++ 4.9.0给出错误,但使用Visual Studio 2013可以正常编译 - initializer list to initialize std::vector<std::function<bool(std::string)> > gives error with g++ 4.9.0 but compiles fine with Visual Studio 2013 MinGW GCC编译错误的代码,没有警告或错误 - MinGW GCC compiles a faulty code without warning or error “Visual Studio中的模板参数无效”错误,但不是GCC - “Invalid template argument” error in Visual Studio but not GCC 从std :: map派生的类不能在Visual C ++上编译(但可以在gcc和clang上编译) - Class derived from std::map does not compile on Visual C++ (but compiles on gcc and clang)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM