简体   繁体   English

Char *阵列内存泄漏

[英]Char* Array Memory Leak

I am having issues de-allocating memory that I used in my char* array. 我在char*char*数组中使用的内存时遇到问题。 In my code snippet below, I am creating a char* array named input that holds pointers to single words at a time followed by a pointer NULL at the end of the array. 在下面的代码片段中,我正在创建一个名为inputchar*数组,该数组一次保存指向单个单词的指针,然后在该数组的末尾保留一个指针NULL This is the only time (I believe) I allocate memory in my code. (我相信)这是我唯一一次在代码中分配内存的时间。

char* input[999];
//exec commands 
for(unsigned int i = 0; i < commands.size(); i++)
{
    string current = "";
    string word = "";
    int k = 0;
    for(unsigned int j = 0; j < commands.at(i).size(); j++) //iterate through letters
    {
        current = commands.at(i);
        //cout << "current: " << current << endl;
        if(current[j] == ' ')
        {
            input[k] = new char[word.size() + 1];
            strcpy(input[k], word.c_str());
            k++;
            word = "";
        }
        else
            word += current[j]; //add letter
        //cout << "word: " << word << endl;
    }
    input[k] = new char[word.size() + 1];
    strcpy(input[k], word.c_str());
    k++;
    input[k] = new char[1]; //add the NULL char *
    input[k] = NULL;
    ...

Later on, I attempt to de-allocate this memory with this snippet of code: 稍后,我尝试使用以下代码片段取消分配此内存:

for(int z = 0; z < k; z++)
{
    delete[] input[z];
}

I am iterating through my char* array and deleting the memory allocated at each index. 我正在遍历char*数组并删除在每个索引处分配的内存。 Without using this snippet, valgrind informs me of memory leaks. 如果不使用此代码段,则valgrind会通知我内存泄漏。 Using this snippet, valgrind informs me of less memory leaks than before. 使用此代码段,valgrind通知我的内存泄漏少于以前。 I am still stuck with the issue of memory still being definitely lost . 我仍然对内存的问题仍然毫无疑问感到困惑

I am unsure what I am missing to remove the rest of the allocated memory (or if the cause of the rest of the memory leaks is in fact somewhere else in my code). 我不确定要删除剩余的分配内存(或者如果导致内存泄漏的原因实际上是我代码中的其他原因),我不确定。 I appreciate any and all help, suggestions, and tips. 我感谢所有帮助,建议和提示。

I think, your problem is in below case, 我认为,您的问题出在以下情况中,

input[k] = new char[1]; //add the NULL char *
input[k] = NULL;

here, without free -ing input[k] , you're assigning NULL to it. 在这里,没有free input[k] ,您正在为其分配NULL Thus, the previous input[k] is lost. 因此,先前的input[k]丢失了。

If you really want input[k] to hold NULL , ( maybe as a sentinel value ), you can simply do 如果您真的想让input[k]保持NULL ,( 也许是一个哨兵值 ),您可以简单地做

input[k] = NULL;

No need to allocate memory separately. 无需单独分配内存。

You don't need to create a new char on where it says input[k] = new char[1]; //add the NULL char * 您不需要在input[k] = new char[1]; //add the NULL char *地方创建新字符input[k] = new char[1]; //add the NULL char * input[k] = new char[1]; //add the NULL char * Just leave the NULL assignment in and it should work fine. input[k] = new char[1]; //add the NULL char *只需保留NULL赋值,它就可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM