c++ - Char* Array Memory Leak -


i having issues de-allocating memory used in char* array. in code snippet below, creating char* array named input holds pointers single words @ time followed pointernull @ end of array. time (i believe) allocate memory in code.

char* input[999]; //exec commands  for(unsigned int = 0; < 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 null char *     input[k] = null;     ... 

later on, attempt de-allocate memory snippet of code:

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

i iterating through char* array , deleting memory allocated @ each index. without using snippet, valgrind informs me of memory leaks. using snippet, valgrind informs me of less memory leaks before. still stuck issue of memory still being definitely lost.

i unsure missing remove rest of allocated memory (or if cause of rest of memory leaks in fact somewhere else in code). appreciate , help, suggestions, , tips.

i think, problem in below case,

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

here, without free-ing input[k], you're assigning null it. thus, previous input[k] lost.

if want input[k] hold null, (maybe sentinel value), can do

input[k] = null; 

no need allocate memory separately.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -