c返回变量和免费的malloc内存

时间:2016-02-25 17:20:16

标签: c malloc

我有一个函数,它接受一个url并返回所请求的文件类型,但我无法弄清楚如何释放并返回malloc' d char *。

const char* lookup(const char* path){
    char* rawEnding;
    char* ending = malloc(strlen(path));
    char* mime = malloc(strlen(path));

    rawEnding = strrchr(path, '.');
    if(strcasecmp(rawEnding, ".css") == 0 || strcasecmp(rawEnding, ".html") == 0 || strcasecmp(rawEnding, ".javascript") == 0 || strcasecmp(rawEnding, ".php") == 0)  {
        memmove(ending, rawEnding+1, strlen(rawEnding));
        sprintf(mime, "text/%s", ending);
        free(ending);
        return mime;
    } else if (strcasecmp(rawEnding, ".gif") == 0 || strcasecmp(rawEnding, ".ico") == 0 || strcasecmp(rawEnding, ".png") == 0) {
        memmove(ending, ending+1, strlen(ending));
        sprintf(mime, "image/%s", ending);
        return mime;
    } else if (strcasecmp(rawEnding, ".jpg") == 0 || strcasecmp(rawEnding, ".jpeg") == 0) {
        return "text/jpeg";
    } else {
        return NULL;
    }
}

2 个答案:

答案 0 :(得分:3)

如果函数在一种情况下返回一个字符串文字,而在另一种情况下返回一个指向动态全部内存的指针,这是一个坏主意。您的函数应始终返回动态分配的内存或NULL。所以你可以free函数之外的内存。

const char* lookup( const char* path )
{
    char* rawEnding = strrchr( path, '.' );
    if ( rawEnding == NULL )
        return NULL;

    char* ending = rawEnding + 1;
    if ( strcasecmp( ending, "css" ) == 0 ||
         strcasecmp( ending, "html" ) == 0 ||
         strcasecmp( ending, "javascript" ) == 0 ||
         strcasecmp( ending, "php" ) == 0)
    {   
        int len = strlen( "text/" ); 
        char* mime = malloc( len + strlen( ending ) + 1 );
        strcpy( mime, "text/" );
        strcpy( mime + len, ending );
        return mime;
    }
    else if ( strcasecmp( ending, "gif" ) == 0 ||
              strcasecmp( ending, "ico" ) == 0 ||
              strcasecmp( ending, "png" ) == 0)
    {
        int len = strlen( "image/" ); 
        char* mime = malloc( len + strlen( ending ) + 1 );
        strcpy( mime, "image/" );
        strcpy( mime + len, ending );
        return mime;
    }
    else if ( strcasecmp( ending, "jpg" ) == 0 ||
              strcasecmp( ending, "jpeg" ) == 0)
    {
        char* mime = malloc( strlen( "text/jpeg" ) + 1 );
        strcpy( mime, "text/jpeg" );
        return mime;
    }
    return NULL;
}

答案 1 :(得分:0)

我看到如果结尾是“jpg”或“jpeg”,则将其转换为“text / jpeg”。这是你的意思吗?

你不能释放mime,因为你想在调用函数中使用它。一旦你完成它的使用,你可以在第一次检查它不是“text.jpg”或NULL之后释放它。

或者,您可以通过一次退货处理它。然后在外面,你可以释放mime,只要它不是NULL。

if(strcasecmp(rawEnding, ".css") == 0 || strcasecmp(rawEnding, ".html") == 0 || strcasecmp(rawEnding, ".javascript") == 0 || strcasecmp(rawEnding, ".php") == 0)  {
    memmove(ending, rawEnding+1, strlen(rawEnding));
    sprintf(mime, "text/%s", ending);
    free(ending);
    // return mime;
} else if (strcasecmp(rawEnding, ".gif") == 0 || strcasecmp(rawEnding, ".ico") == 0 || strcasecmp(rawEnding, ".png") == 0) {
    memmove(ending, ending+1, strlen(ending));
    sprintf(mime, "image/%s", ending);
    // return mime;
} else if (strcasecmp(rawEnding, ".jpg") == 0 || strcasecmp(rawEnding, ".jpeg") == 0) {
    strcpy(mime, "text/jpeg");
} else {
    free(mime);
    mime = NULL;
}
return mime;