【发布时间】:2015-06-14 05:52:51
【问题描述】:
我正在尝试在 g++ 中编译和运行这个看起来无害的类,但程序在 malloc 或 memmove 上不断出现段错误。这里几乎没有发生任何事情。
使用 -g 编译并通过 gdb 运行时,我收到以下错误。这就是我对如何调试的了解程度。
由于某种原因,只有在从两个单独的文件编译时才会出现段错误。如果我将所有内容复制/粘贴到 main.cpp 中,它运行良好。
Windows/Cygwin g++ (GCC) 4.9.2:
程序收到信号SIGSEGV,分段错误。
0x61137eb5 in memmove() from /usr/bin/cygwin1.dll
..
程序收到信号SIGSEGV,分段错误。
0x610ef417 in muto::acquire(unsigned long) () from /usr/bin/cygwin1.dll
Windows/g++ 4.9.2-TDM:
程序收到信号SIGSEGV,分段错误。
0x777d27d0 in ntdll!RtlCompareMemoryUlong () from /c/Windows/SysWOW64/ntdll.dll
程序收到信号SIGSEGV,分段错误。 0x00007ffff7279ef5 in _int_malloc () from /lib64/libc.so.6 缺少单独的调试信息,使用:debuginfo-install glibc-2.17-55.el7_0.5.x86_64 libgcc-4.8.2-16.2.el7_0.x86_64 libstdc++-4.8.2-16.2.el7_0.x86_64 ..
CentOS 7/g++ (GCC) 4.8.2:
程序收到信号SIGSEGV,分段错误。
_int_malloc (av=0x7ffff75b6760 , bytes=29) at malloc.c:3281
3281 {
我正在使用以下标志进行编译:
-pedantic -Wall -Wextra -Wcast-align -Wcast-qual -Wctor-dtor-privacy -Wdisabled-optimization -Wformat=2 -Winit-self -Wlogical-op -Wmissing-declarations -Wmissing-include-dirs -Wnoexcept -Woverloaded-virtual -Wredundant-decls -Wshadow -Wsign-conversion -Wsign-promo -Wstrict-null-sentinel -Wstrict-overflow=5 -Wswitch-default -Wundef -Wno-unused
我错过了什么?我还能在 gdb 中做些什么来帮助查明问题吗?
$ g++ main.cpp string_t.cpp -g -std=c++11 $DEBUG -o test.exe && ./test.exe
main.cpp
#include <iostream>
using std::cout;
using std::endl;
#include "string_t.h"
int main() {
string_t a("test");
cout << a << endl;
return 0;
}
string_t.h
#include <cstdint>
#include <string>
class string_t {
std::basic_string<char> _base;
public:
string_t(
const char* s);
friend std::ostream& operator<<(
std::ostream& l,
const string_t& r);
};
string_t.cpp
#include "string_t.h"
string_t::string_t(
const char* s) :
_base(s) {
}
std::ostream& operator<<(
std::ostream& l,
const string_t& r)
{
l << r._base.c_str();
return l;
}
【问题讨论】:
-
你的代码用 g++ 编译得很好(在添加了缺少的
#include <cstdint>之后)。 -
该标准不要求实现为
unsigned char提供char_traits的专门化(几乎可以肯定uint8_t的类型定义为)。我很惊讶它会出现段错误,它在我自己的 mingw + gcc 主干构建上运行良好,但从技术上讲,你在无人区,你应该改变你的设计,而不是浪费时间试图找出哪里出错了。 -
将所有的 uint8_t 更改为字符,它仍然是 segfauls 但在不同的地方。不过猜对了。
-
您仍然缺少
#include <iostream>包含在string_t.h中。 -
@Zhro 是的,如果您实际上发布了导致问题的确切代码,那么您会更快得到答案。关于
char_traits的观点仍然存在,这不是可移植的。
标签: c++ segmentation-fault cygwin