简体   繁体   English

正确使用Malloc和免费使用C ++指针

[英]Correct use of Malloc and free with c++ pointers

Sorry if this question comes across as trying to take the easy way out, but I really think I just need some clarification. 很抱歉,如果遇到这个问题是想采取简单的方法,但是我真的认为我只需要澄清一下。 I'm trying to use malloc and free. 我正在尝试使用malloc和free。 I thought I understood them, but the assignment itself that I'm doing them for is somewhat confusing. 我以为我理解它们,但是我为它们做的作业本身有些令人困惑。

Each element in the tklist array is supposed to point to an individual token (set of characters either between quotes or separated by spaces). tklist数组中的每个元素都应指向一个单独的标记(引号之间或由空格分隔的字符集)。 So, I need to allocate the space and then also free it. 因此,我需要分配空间,然后再释放它。

What I don't understand is when the memory is supposed to be freed. 我不明白的是何时应该释放内存。 I've tried freeing it in all sorts of places and nothing works. 我尝试过在各种地方释放它,但是没有任何效果。 I think I just don't fully understand something, and I've looked up malloc and free extensively for the last couple of days trying to figure it out.(Maybe I'm supposed to free in my main? maybe my strncpy is wrong?) Any help would be really appreciated! 我想我只是不完全了解某些东西,并且在过去的几天里我一直在广泛地查找malloc和free以试图弄清楚它。(也许我应该在我的主程序中自由吗?也许我的strncpy是错误的) ?) 任何帮助将非常感激!

Here is the function, it takes as parameters an array of characters and an array of char pointers and returns as its answer the number of tokens that were found in the first parameter. 这是该函数,它将一个字符数组和一个char指针数组作为参数,并返回在第一个参数中找到的令牌数作为其答案。 The function should parse the tokens and add each one in turn to the tklist array. 该函数应解析标记并将每个标记依次添加到tklist数组中。

int parseCommandLine(char cline[], char *tklist[]){
    int i;
    int length;
    int count = 0; //counts number of tokens
    int toklength = 0; //counts the length of each token
    length = strlen(cline);
    for (i=0; i < length; i++) {   //go to first character of each token

        if (((cline[i] != ' ' && cline[i-1]==' ') || i == 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] == '"') {
            do {
                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;



}

使用完后,您需要在调用方释放tklist。

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

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