C ++ malloc()内存损坏(快)

时间:2016-04-18 03:12:42

标签: c++ c++11 memory-leaks out-of-memory

我对编程很新,我的程序存在内存问题。某处我过度使用记忆,但找不到来源。我不明白为什么它给我malloc分配的问题,因为我不动态分配任何变量。感谢

//returns the index of the character in the string
int find(string line, int begin, int end, char character) {
    for (int i = begin; i <= end; i++) {
        if (line[i] == character) {
            return i;
        }
    }

    //return -1 if not found
    return -1;
}

//Get the characters from levelorder that align with inorder
char* getCharacters(char inOrder[], char levelOrder[], int a, int b) {
    char *newLevelOrder = new char[a];
    int j = 0;
    for (int i = 0; i <= b; i++)
        if (find(inOrder, 0, a-1, levelOrder[i]) != -1)
            newLevelOrder[j] = levelOrder[i], j++;
    return newLevelOrder;
}

//creates a new Node given a character
Node* newNode(char character) {
    Node *node = new Node;
    node->character = character;
    node->left = NULL;
    node->right = NULL;
    return node;
}

//creates the huffman tree from inorder and levelorder
Node* createInLevelTree(char inOrder[], char levelOrder[], int beginning, int end, int size) { 
    //if start index is out of range
    if (beginning > end) {
        return NULL;
    }

    //the head of the tree is the 1st item in level order's traversal
    Node *head = newNode(levelOrder[0]);

    //if there are no children we can't go farther down
    if (beginning == end) {
        return head;
    }

    //get the index of the node
    int index = find(inOrder, beginning, end, head->character);

    //get the subtree on the left
    char *leftTree  = getCharacters(inOrder, levelOrder, index, size);

    //get the subtree on the right
    char *rightTree  = getCharacters(inOrder + index + 1, levelOrder, size-index-1, size);

    //branch off to the left and right
    head->left = createInLevelTree(inOrder, leftTree, beginning, index-1, size);
    head->right = createInLevelTree(inOrder, rightTree, index+1, end, size);

    //delete
    delete [] leftTree;
    delete [] rightTree;

    return head;
 }

已修复此行。谢谢山姆。     Char * new level order = new char [b]

1 个答案:

答案 0 :(得分:0)

  

某处我过度使用记忆,但找不到来源。

我建议你至少用std::vector<char>std::string替换你的字符数组并输入一些大小的断言,或者使用at成员看不会发生过度索引。此外,使用operator new很可能是在malloc方面实现的,而operator是在free方面删除的。因此,您是动态分配的。

此外, RAII 的维基。尝试使用 RAII 来动态分配内存......总是如此。 std :: vector和std :: string 免费提供此功能。

另外,请考虑以下代码:

char* getCharacters(char inOrder[], char levelOrder[], int a, int b) {
    char *newLevelOrder = new char[a];
    int j = 0;
    for (int i = 0; i <= b; i++)
        if (find(inOrder, 0, a-1, levelOrder[i]) != -1)
            newLevelOrder[j] = levelOrder[i], j++;
    return newLevelOrder;
}

读到这个,我不确定b的数量。通话时没有限制。我怎么知道for循环不会调用不确定的行为(通过过度索引)。通常,正确的for循环在这里使用“a”,因为“a”用于创建数组...如果你想像这样编码,请自由地使用asserts,因为你正在对调用代码做出假设(但只是使用一个载体....)。

    char *newLevelOrder = new char[a];
    int j = 0;

    for (int i = 0; (i < a) && (i <= b); i++)
    {

    assert (b < a);
    char *newLevelOrder = new char[a];
    int j = 0;

    for (int i = 0; (i <= b); i++)
    {

我的任务是用向量和字符串替换你的数组作为练习,以及在提到的循环中自由地喷射断言......这可能会解决你的问题