c++ - 无法解释的 C++ 段错误

标签 c++ segmentation-fault

我在 C++ 中试验对象时遇到了段错误。其中没有我创建的指针。所以我不知道出了什么问题。

#include <iostream>
#include <iomanip>
#include <locale>
#include <sstream>
#include <string>
using namespace std;

class test {
public:
  test(string hw, int tree, string vate);
  ~test() {

  }
  string helloworld;
  int three;
  string privacy() {
    cout << "I will now access the  private variable PRI\n";
    cout << "pri = private\n";
    cout << "The value of PRI is "+pri+"\n";
  }
private:
  string pri;
};

test::test (string hw, int tree, string vate) {
helloworld = hw;
three = tree;
pri = vate;
}

int main() {
  cout << "We will now perform an object test!\n";
  test thing ("hello world", 3, "private");
  cout << "thing.helloworld\n";
  cout << "hello world\n";
  cout << "Real value: "+thing.helloworld+"\n";
  cout << "thing.three\n";
  cout << "3\n";
  ostringstream thee;
  thee << thing.three;
  string result = thee.str();
  cout << "Real value: "+result+"\n";
  cout << "thing.privacy()\n";
  cout << "Real value:\n";
  thing.privacy();
  return 0;
}

这是我遇到的错误:

We will now perform an object test!
thing.helloworld
hello world
Real value: hello world
thing.three
3
Real value: 3
thing.privacy()
Real value:
I will now access the  private variable PRI
pri = private
The value of PRI is private
*** glibc detected *** ./objecttest: free(): invalid pointer: 0xbfd0d8ac ***
======= Backtrace: =========
/lib/i386-linux-gnu/i686/cmov/libc.so.6(+0x70f01)[0xb7568f01]
/lib/i386-linux-gnu/i686/cmov/libc.so.6(+0x72768)[0xb756a768]
/lib/i386-linux-gnu/i686/cmov/libc.so.6(cfree+0x6d)[0xb756d81d]
/usr/lib/i386-linux-gnu/libstdc++.so.6(_ZdlPv+0x1f)[0xb76ec4bf]
/lib/i386-linux-gnu/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0xb750ee46]
./objecttest[0x8048bb1]
======= Memory map: ========
08048000-0804a000 r-xp 00000000 08:01 782387     /home/jacob/Coding/cpptests/objecttest
0804a000-0804b000 rw-p 00001000 08:01 782387     /home/jacob/Coding/cpptests/objecttest
08098000-080b9000 rw-p 00000000 00:00 0          [heap]
b7300000-b7321000 rw-p 00000000 00:00 0 
b7321000-b7400000 ---p 00000000 00:00 0 
b74f6000-b74f8000 rw-p 00000000 00:00 0 
b74f8000-b7654000 r-xp 00000000 08:01 655678     /lib/i386-linux-gnu/i686/cmov/libc- 2.13.so
b7654000-b7655000 ---p 0015c000 08:01 655678     /lib/i386-linux-gnu/i686/cmov/libc-2.13.so
b7655000-b7657000 r--p 0015c000 08:01 655678     /lib/i386-linux-gnu/i686/cmov/libc-2.13.so
b7657000-b7658000 rw-p 0015e000 08:01 655678     /lib/i386-linux-gnu/i686/cmov/libc-2.13.so
b7658000-b765b000 rw-p 00000000 00:00 0 
b765b000-b7677000 r-xp 00000000 08:01 651524     /lib/i386-linux-gnu/libgcc_s.so.1
b7677000-b7678000 rw-p 0001b000 08:01 651524     /lib/i386-linux-gnu/libgcc_s.so.1
b7678000-b7679000 rw-p 00000000 00:00 0 
b7679000-b769d000 r-xp 00000000 08:01 655675     /lib/i386-linux-gnu/i686/cmov/libm-2.13.so
b769d000-b769e000 r--p 00023000 08:01 655675     /lib/i386-linux-gnu/i686/cmov/libm-2.13.so
b769e000-b769f000 rw-p 00024000 08:01 655675     /lib/i386-linux-gnu/i686/cmov/libm-2.13.so
b769f000-b777f000 r-xp 00000000 08:01 1046437    /usr/lib/i386-linux-gnu/libstdc++.so.6.0.17
b777f000-b7783000 r--p 000e0000 08:01 1046437    /usr/lib/i386-linux-gnu/libstdc++.so.6.0.17
b7783000-b7784000 rw-p 000e4000 08:01 1046437    /usr/lib/i386-linux-gnu/libstdc++.so.6.0.17
b7784000-b778b000 rw-p 00000000 00:00 0 
b77a0000-b77a3000 rw-p 00000000 00:00 0 
b77a3000-b77a4000 r-xp 00000000 00:00 0          [vdso]
b77a4000-b77c0000 r-xp 00000000 08:01 651542     /lib/i386-linux-gnu/ld-2.13.so
b77c0000-b77c1000 r--p 0001b000 08:01 651542     /lib/i386-linux-gnu/ld-2.13.so
b77c1000-b77c2000 rw-p 0001c000 08:01 651542     /lib/i386-linux-gnu/ld-2.13.so
bfcee000-bfd0f000 rw-p 00000000 00:00 0          [stack]
Aborted

指针 0xbfd0d8ac 指向什么?

最佳答案

您的 string privacy() 返回一个 string,但您从不返回任何内容。在返回值函数中流出函数末尾是未定义的行为。也许您希望它是 void privacy()

此外,我建议使用 -Wall 进行编译。您会收到编译器警告,[-Wreturn-type] 立即告诉您问题所在:

warning: no return statement in function returning non-void [-Wreturn-type]

关于c++ - 无法解释的 C++ 段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18166650/

相关文章:

c++ - 如何在 winsock 中使 'send' 非阻塞

c++ - 字符串格式异常 PRIu64

python - pip lxml 安装段错误

C++二叉树递归析构函数问题

c++ - 就地快速基数排序实现

c++ - 如何将 Qt 与应用程序打包(但不是静态的)

c++ - 在不丢失分隔符的情况下拆分字符串

python - gc 上的段错误,使用 ctypes

c - 从 void ptr 取消引用整数时出现 SegFault

java - GWT 编译导致 SIGSEGV