带 const char * 和 al_get_native_file_dialog_path() 的内存管理

Memory management w/ const char * and al_get_native_file_dialog_path()

我正在使用 returns 一个 const char * 变量的库函数。下面是代码:

if (something) {
     const char* file = get_filename();
     save(file);
}

是否需要释放块内的文件变量,因为它在块中?

我使用的函数是 al_get_native_file_dialog_path(),来自 allegro 5 库。

我试图搜索有关如何分配 const char * 变量的任何文档,但一无所获..

Is there any need to deallocate the file variable inside the block, since it's in a block?

作用域块内的 (const) 指针仍然只是一个指针。

当范围离开时,没有采取任何特殊操作(例如自动释放它指向的内存)。

所以我们无法真正判断除非知道该指针是如何分配的

const char* file = get_filename();

可能类似于

const char* get_filename() {
    static const char* hardcoded_stuff = "TheFileName.txt";
    return hardcoded_stuff;
}

const char* get_filename() {
    const char* filename = new char[MAXFILENAME];
    // ... determine and initialize filename ...
    return filename;
}

第一个版本不需要从客户端释放内存,而第二个版本需要。


I'm using a library function ...

表示您正在使用一些不受您控制的代码。所以你必须参加那个库的文档,或者问作者。


I've tried to search for any documentation on how the const char * variable is allocated, but nothing..

嗯,你检查过他们的 example in the documentation 了吗?

我没有发现任何代码来释放通过 al_get_native_file_dialog_path() 获得的指针。