Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 内存管理w/const char*和al_get_native_file_dialog_path()_C++_Char_Constants_Allegro5 - Fatal编程技术网

C++ 内存管理w/const char*和al_get_native_file_dialog_path()

C++ 内存管理w/const char*和al_get_native_file_dialog_path(),c++,char,constants,allegro5,C++,Char,Constants,Allegro5,我正在使用一个库函数,它返回一个const char*变量。代码如下: if (something) { const char* file = get_filename(); save(file); } 由于文件变量位于块中,是否需要在块内取消分配该文件变量 我使用的函数是allegro 5库中的al\u get\u native\u file\u dialog\u path() 我试图搜索任何关于如何分配const char*变量的文档,但什么都没有 由于文件变量位于块中

我正在使用一个库函数,它返回一个const char*变量。代码如下:

if (something) {
     const char* file = get_filename();
     save(file);
}
由于文件变量位于块中,是否需要在块内取消分配该文件变量

我使用的函数是allegro 5库中的
al\u get\u native\u file\u dialog\u path()

我试图搜索任何关于如何分配
const char*
变量的文档,但什么都没有

由于文件变量位于块中,是否需要在块内取消分配该文件变量

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

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

因此,除非知道指针是如何分配给

const char* file = get_filename();

可能是这样的

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

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


我正在使用一个库函数

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


我试图搜索任何关于如何分配const char*变量的文档,但什么都没有

嗯,你查过他们的照片了吗


我没有发现任何代码来释放通过
al\u get\u native\u file\u dialog\u path()

获取的指针。字符串是如何分配的?您需要查看库文档,了解如何管理返回的指针。这里没有人知道您的库。
get\u filename
的文档说明了什么?您必须询问get\u filename的作者。光看电话号码是不可能知道的code@ventur65好吧,至少你可以使用valgrind这样的工具来发现内存是否是动态分配的,并且在不删除的情况下泄漏。我使用的函数是allegro库中的al_get_native_file_dialog_path()。我试图搜索有关如何分配const char*变量的任何文档,但没有找到任何东西..@Venture65。。。但什么都没有。那么,从他们的文档中呢?我无法从他们的示例代码中发现任何内存释放。@Venture65如果您查看,您可以看到
al_get_native_file_path()
返回指向对话框a字段的指针。对话框将保留路径的所有权。
const char* get_filename() {
    const char* filename = new char[MAXFILENAME];
    // ... determine and initialize filename ...
    return filename;
}