delete []导致堆损坏 [英] delete[] causing heap corruption

查看:87
本文介绍了delete []导致堆损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很清楚这样的问题不计其数,但是我搜索了数小时却无法理解我做错了什么,因此,非常感谢您的帮助.(我是编程新手)

I'm well aware that there are countless problems like this, but I searched for hours and couldn't understand what I did wrong so I would really appreciate your help. (I'm new to programming)

作为作业的一部分,我需要创建各种字典管理器,但是删除单词似乎有问题.我收到一条错误消息"...触发了断点".

I need to create a dictionary manager of sorts as part of my homework but I seem to have a problem with deleting words. I get an error message "...triggered a breakpoint".

人们对这个问题的通常答案是这是由于越界导致的堆损坏,但我不知道是否以及如何造成了此问题.

The usual answer people get to this problem is that this is heap corruption caused by going out of bounds but I can't see if and how I caused this.

我已经用公交信息管理做了一些类似的事情,并且它工作得非常好,这使我更加困惑……(显然,我没有使该机制完全相同,但是即使看了我之前的代码,我也无法做到)找出问题所在

I already made something similar with bus info management and it worked perfectly so that makes me even more confused... (Obviously, I did not make the mechanism exactly the same, but even after looking at my previous code I couldn't isolate the problem)

我添加了我认为值得关注的功能

I added the functions I believe are of concern,

添加功能:

void Add_Word(char**& dictionary, int& dictionary_size, char word[])
{
    char** temp = new char*[dictionary_size + 1];   // Create a new array of appropriate size.

    int i;
    for (i = 0; i < dictionary_size; i++)
    {
        temp[i] = dictionary[i];    // Copy head pointers addresses for all existing items.
    }
    temp[i] = new char[strlen(word)];   // Add the space for the new word,
    temp[i][strlen(word)] = '\0';   // mark its end

    strcpy_s(temp[i], strlen(word) + 1, word);  // then copy it.
    // I'm really not so sure about what I should put in the buffer length but
    // strlen(word) + 1 seemed to work... I know... not good, but strlen(word) alone caused a problem.

    if (dictionary_size > 0)
        delete []dictionary;    // Delete previous head pointers array if there are any and
    dictionary = temp;  // reset the main pointer to the address of the new one.

    dictionary_size++;  // Finally, increase dictionary_size.
}

删除功能:

void Delete_Word(char**& dictionary, int& dictionary_size, char* word)
{
    // !!! This is where the crash thingy happens.
    delete[] Search_For_Word(dictionary, dictionary_size, word);    // Delete the word from the dictionary.
    // Search_For_Word returns a pointer to the word it receives, from the dictionary.

    char** temp = new char*[dictionary_size - 1];   // Create a new array of appropriate size.

    int i;
    for (i = 0; i < dictionary_size; i++)
    {
        if (dictionary[i][0])
            temp[i] = dictionary[i];    // Copy the head pointers of the existing
           // items to the new array except for the deleted word.
    }

    delete[] dictionary;    // Delete previous head pointers array and
    dictionary = temp;  // reset the main pointer to the address of the new one.

    dictionary_size--;  // Finally, decrease dictionary_size.
}

效率过低或明显损坏的任何部分都可能是由于我弄乱了我的代码,试图自己找出来的原因(例如,调用了3次strlen被提到(再次感谢,kfsone)...),或忘记为其+1标记'\ 0'以标记字符串的结尾-实际上,不,如果我们很显然你不会告诉我我的错误@.@).

Any parts that are excessively inefficient or obviously broken are likely a result of me messing with my code trying to figure this out on my own (such as the calling 3 times to strlen mentioned (thanks again for that, kfsone...), or forgetting to +1 it for the '\0' to mark the end of a string --actually, no, if we go by obvious you won't tell me my mistakes @.@).

由于我要处理char而不是字符串和向量的原因,请允许我自己引用:"...作为我的家庭作业的一部分".我才刚刚开始编程.那,我想在继续使用更舒适的上层工具之前掌握基础知识.

As for the reason I'm dealing with char instead of strings and vectors please allow me to quote myself: "...as part of my homework". I just barely started programming. That, and I want to grasp the basics before moving on to using the more comfortable higher-up tools.

推荐答案

代码现已正常运行.

到处都是错的.在尝试修复动态内存时,我几乎弄乱了我可以解决的有关动态内存的任何部分.

It was wrong all over. I messed up pretty much any part that I could regarding the dynamic memory while trying to fix it before.

起初我并不在意打3次电话,因为这只是个作业和一个很小的程序,但我想习惯以正确的方式做事会更好...我还删除了我显然不太了解的副本,而赞成使用简单的for循环.

I initially didn't care about calling 3 times to strlen becuase it's just homework and a very small program but I guess it's better to get used to do things the right way... I also dropped the copy which I evidently don't understand very well in favour of a simple for loop.

// Add function. The rest is cut.
    int word_length = strlen(word);

    temp[i] = new char[word_length + 1];    // Added +1 here.
    temp[i][word_length] = '\0';    /* This was correct after all.
    the word_length index is the correct ending.*/

    for (int j = 0; j < word_length; j++)   // copy replaced by for loop.
        temp[i][j] = word[j];
    // cut
}

void Delete_Word(char**& dictionary, int& dictionary_size, char* word)
{
    delete[] Search_For_Word(dictionary, dictionary_size, word);
   // There was a -1 mistake here I made in order to try and fix the thing earlier.
// No need for more, it works perfectly now.

这篇关于delete []导致堆损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆