c++ - 程序在测试期间被杀死

标签 c++ linux memory out-of-memory kill

当我运行我的程序时,我收到消息 Killed 以及一些关于脚本的信息。在对该问题进行一些研究之后,我发现我并没有删除动态分配的变量(愚蠢的我!)。但是,现在,我觉得我已经解决了这个问题,但是当我使用 Linux 时,我仍然在终端中收到 Killed 消息。

    //does the of the manipulation of the load factor.
    for (int tableSize = fileLength; tableSize < fileLength * 2; tableSize = tableSize + 500)
    {   

        //creates hash tables to be reused for each of the trials.
        for(int fileNum = 0; fileNum < NUMTIMES; fileNum++)
        {   

            Array_HashTable* linear_div_hash = new Array_HashTable(tableSize);
            LinkedList_HashTable *chain_div_hash = new LinkedList_HashTable(tableSize);

            Array_HashTable *doubleHash = new Array_HashTable(tableSize);        
            LinkedList_HashTable *mult_hash = new LinkedList_HashTable(tableSize);
            //Does the hashing for each of the files created.
            for (int index = 0; index < fileLength; index++)        
            {
                linear_div_hash -> Linear_ProbeDH(read[fileNum][index]);
                chain_div_hash ->  Division_Hash(read[fileNum][index]);
                doubleHash -> Double_Hash(read[fileNum][index]);
                mult_hash -> Mulitplication_Hash(read[fileNum][index]);
            }//ends the index for loop.

            optimalOutput("VariableSizeLinearCollisionData", fileLength, tableSize, linear_div_hash -> getCollisions(), fileAppendage);
            optimalOutput("VariableSizeDoubleCollisionData", fileLength, tableSize, doubleHash -> getCollisions(), fileAppendage);
            optimalOutput("VariableSizeDivisionChainingCollisionData", fileLength, tableSize, chain_div_hash -> getCollisions(), fileAppendage);
            optimalOutput("VariableSizeMultiplicationChainingCollisionData", fileLength, tableSize, mult_hash -> getCollisions(),fileAppendage);    

            linear_div_hash -> EndArray_HashTable(); 
            chain_div_hash-> EndLinkedList_HashTable();
            doubleHash -> EndArray_HashTable();
            mult_hash-> EndLinkedList_HashTable();

            delete  linear_div_hash; 
            delete  chain_div_hash ;
            delete  doubleHash ;
            delete  mult_hash ;
        }//ends the fileNum for loop
    }//ends the parent for loop with the size as the variable.

基本上代码是这样工作的,第一个 for 循环控制哈希表的大小。第二个循环控制将使用哪个文件的数据进行哈希处理。并且为此实例化了一个哈希表对象。最后一个循环调用哈希函数。然后使用输出函数将统计信息输出到文件中。然后我使用类似于析构函数的函数从我的类中删除动态变量。我不能使用析构函数来执行此操作,因为它给我带来了错误。然后我删除对象。

我能做什么?

最佳答案

如果你在 linux 上运行,你可以使用 valgrind

valgrind myprogram

会很慢但是会报很多内存问题。如果还是找不到,可以使用 massif 堆剖析

valgrind --tool=massif myprogram
ms_print <profile_output_file>

这将生成一个内存使用情况图和几个快照时刻的最大内存分配(包括它们分配位置的精确堆栈跟踪)。

哦,使用 gcc -g 构建调试信息

关于c++ - 程序在测试期间被杀死,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5517756/

相关文章:

linux - 如何在 linux 中增加 QT Gui 线程优先级

Javascript 垃圾收集器不清除对象

java - Android展开JNI本地引用表

c++ - 如何正确处理 OpenSSL 错误 (BIO)

C++ 从函数、指针返回多维数组的最佳方式?

python - 在没有 apt-get 的 Linux (Ubuntu) 上运行 OpenCV

c - 基本内存地址混淆

c++ - C++中的二进制文件

c++ - 从音频缓冲区和视频缓冲区 C++ Windows 构造 mp4 文件

linux - Bash - while 循环语法错误