c 返回变量和空闲内存

  • 本文关键字:内存 返回 变量 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;
    }
}

如果函数在一种情况下返回字符串文本,而在另一种情况下返回指向动态所有内存的指针,则这是一个坏主意。您的函数应始终返回动态分配的内存或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;
}

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

您无法释放 MIME,因为您想在调用函数中使用它。使用它完成后,您可以在首先检查它不是"text.jpg"或 NULL 后释放它。

或者,您可以使用一次退货来处理它。然后在外面,你可以释放哑剧,只要它不是 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;

相关内容

  • 没有找到相关文章

最新更新