Correct use of Malloc and free with c++ pointers -


sorry if question comes across trying take easy way out, think need clarification. i'm trying use malloc , free. thought understood them, assignment i'm doing them confusing.

each element in tklist array supposed point individual token (set of characters either between quotes or separated spaces). so, need allocate space , free it.

what don't understand when memory supposed freed. i've tried freeing in sorts of places , nothing works. think don't understand something, , i've looked malloc , free extensively last couple of days trying figure out.(maybe i'm supposed free in main? maybe strncpy wrong?) appreciated!

here function, takes parameters array of characters , array of char pointers , returns answer number of tokens found in first parameter. function should parse tokens , add each 1 in turn tklist array.

int parsecommandline(char cline[], char *tklist[]){     int i;     int length;     int count = 0; //counts number of tokens     int toklength = 0; //counts length of each token     length = strlen(cline);     (i=0; < length; i++) {   //go first character of each token          if (((cline[i] != ' ' && cline[i-1]==' ') || == 0)&& cline[i]!= '"') {                while ((cline[i]!=' ')&& (cline[i] != '\0') && (cline[i] != '\r')){                 toklength++;                 i++;             }         count ++;           //---------------         //tklist[count] = (char *) malloc( toklength +1);         //strncpy(tklist[count], &cline[i], toklength);           //  free( (void *)tklist[count] );          //--------------              toklength = 0;         }         //--------------         //free( (void *)tklist[count] );         //--------------         if (cline[i] == '"') {             {                 toklength++;                 i++;                 if (cline[i] == ' ') {                     toklength--;                 }             } while (cline[i]!='"');             count++;             //--------------             //tklist[count] = (char *) malloc( toklength +1);             //strncpy(tklist[count], &cline[i], toklength);             //free( (void *)tklist[count] );             //--------------             toklength = 0;         }         //--------------         //free( (void *)tklist[count] );         //--------------     }     cout << count;     return count;    } 

you need free tklist @ caller side once done using it.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -