C++valgrind错误消息分段错误



我是C++的新手,正在尝试编写一个谜机模拟器。我知道我的指针做了一些奇怪的事情,我已经在Valgrind上运行过了,但我不确定错误信息是什么意思,从哪里开始修复它?(泄漏摘要中的抑制意味着什么?(

以下是代码的一部分,每个组件都是在其中创建的,错误发生在哪里。

Enigma::Enigma(int argc, char** argv){

errorCode = NO_ERROR;
plugboard = NULL;
*rotor = NULL;   //WHERE THE ERROR OCCURS
reflector = NULL;
rotorCount = 0;

//first check how many rotors there are
if (argc >= 5)
rotorCount = argc - 4;
if (argc <= 4)       
errorCode = INSUFFICIENT_NUMBER_OF_PARAMETERS;   
//pass files into each component and check if well-formed
if (errorCode == NO_ERROR){
plugboard = new Plugboard(argv[1]);
errorCode = plugboard -> errorCode;
if (errorCode == NO_ERROR){
cout << "Plugboard configuration loaded successfully" << endl;
reflector = new Reflector(argv[2]);
errorCode = reflector -> errorCode;
if (errorCode == NO_ERROR){
cout << "Reflector configuration loaded successfully" << endl;
rotor = new Rotor*[rotorCount];
size_t i = 0;
while (i < rotorCount && errorCode == NO_ERROR) {
rotor[i] = new Rotor (argv[i+3]);
i++;
errorCode = rotor[i]-> errorCode;

//destructor if rotor loading was unsuccessful 
if (errorCode != NO_ERROR){
for (int j=0; j<=i; j++)
delete rotor[j];
delete [] rotor;    

这是Valgrind错误信息:

reflectors/I.rf rotors/I.rot rotors/II.rot rotors/III.rot rotors/I.pos
==68943== Memcheck, a memory error detector
==68943== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==68943== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info
==68943== Command: ./enigma plugboards/I.pb reflectors/I.rf rotors/I.rot rotors/II.rot rotors/III.rot rotors/I.pos
==68943== 
--68943-- run: /usr/bin/dsymutil "./enigma"
==68943== Use of uninitialised value of size 8
==68943==    at 0x100002598: Enigma::Enigma(int, char**) (enigma.cpp:17)
==68943==    by 0x100002F92: Enigma::Enigma(int, char**) (enigma.cpp:12)
==68943==    by 0x100000862: main (main.cpp:18)
==68943==  Uninitialised value was created by a stack allocation
==68943==    at 0x1000007D4: main (main.cpp:11)
==68943== 
==68943== Invalid write of size 8
==68943==    at 0x100002598: Enigma::Enigma(int, char**) (enigma.cpp:17)
==68943==    by 0x100002F92: Enigma::Enigma(int, char**) (enigma.cpp:12)
==68943==    by 0x100000862: main (main.cpp:18)
==68943==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==68943== 
==68943== 
==68943== Process terminating with default action of signal 11 (SIGSEGV)
==68943==  Access not within mapped region at address 0x0
==68943==    at 0x100002598: Enigma::Enigma(int, char**) (enigma.cpp:17)
==68943==    by 0x100002F92: Enigma::Enigma(int, char**) (enigma.cpp:12)
==68943==    by 0x100000862: main (main.cpp:18)
==68943==  If you believe this happened as a result of a stack
==68943==  overflow in your program's main thread (unlikely but
==68943==  possible), you can try to increase the size of the
==68943==  main thread stack using the --main-stacksize= flag.
==68943==  The main thread stack size used in this run was 10022912.
==68943== 
==68943== HEAP SUMMARY:
==68943==     in use at exit: 18,685 bytes in 166 blocks
==68943==   total heap usage: 187 allocs, 21 frees, 27,133 bytes allocated
==68943== 
==68943== LEAK SUMMARY:
==68943==    definitely lost: 0 bytes in 0 blocks
==68943==    indirectly lost: 0 bytes in 0 blocks
==68943==      possibly lost: 72 bytes in 3 blocks
==68943==    still reachable: 200 bytes in 6 blocks
==68943==         suppressed: 18,413 bytes in 157 blocks
==68943== Rerun with --leak-check=full to see details of leaked memory
==68943== 
==68943== For counts of detected and suppressed errors, rerun with: -v
==68943== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 1 from 1)
Segmentation fault: 11

感谢

也许您应该看看:指针对指针在C中是如何工作的?

基本上,我看到rotor是一个指向指针的指针,或者可能是一个指针数组(因为您将*rotor初始化为NULL,稍后还会设置rotor[i] = new Rotor(。

请确保已正确初始化rotor。它是否指向一个有效的对象?否则,就不能指望*rotort = NULL /* or whatever value */;工作

基本上suppressed意味着内存泄漏到共享库中代码之外。和*rotor = NULL,但它没有指向任何有效的对象。。

最新更新