0
C++의 개체를 실험하고 있었고 분할 오류가 발생했습니다. 내가 만든 포인터가 없습니다. 그래서 나는 무엇이 잘못되었는지 전혀 모른다. 설명 할 수없는 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-b00 rw-p 00000000 00:00 0
b00-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 포인트를 않습니다에?
+1 당신은 이유를 알고 있나요 이것은 컴파일 오류로 취급되지 않습니까? – Mahesh
@Mahesh : 컴파일러는 코드가 함수의 끝에서 벗어나는 지 항상 판단 할 수는 없으며 함수는 예를 들어 예외를 통해 종료 할 수 있습니다. 모든 코드 경로가 값을 반환하는지 여부를 항상 결정할 수 없기 때문에 C++ 표준은 정의되지 않은 동작을 만듭니다. 그러나 MSVC 컴파일러는 경고 IIRC 대신 컴파일러 오류를 표시합니다. –
이것은 Emacs에서 C++을 작성할 수 있다고 생각해서 얻은 것입니다. @ JesseGood 감사합니다! – jacob1031