如何在c ++中删除const数组

Mik*_*kin -3 c++

我是C++的新手.我的代码读取了数千张图像并执行图像处理.我想在函数中读取原始图像,所以我将带有它名称的字符串传递给函数,然后创建常量char数组(只要我需要const char fopen).在函数结束时,我想删除它.但这引发了一个我无法解决的异常:

extern "C" void imreadTIFF(string location, int x, int y){
    const char * c = location.c_str();
    <code>
    delete [] c;
}


Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
Run Code Online (Sandbox Code Playgroud)

我知道,这个问题被多次提出过.但我没有找到如何解决我的问题,也已经尝试了几种方法.

谢谢,米哈伊尔

Bau*_*gen 6

std::string::c_str返回指向内部char数组的指针string.用户不能删除它,因为string-object本身将管理其生命周期.

此外,如果您不打算修改它string,请将其传递给它const string&.如果你确实修改了string-object,请注意strings 上的某些操作使.c_str()指针无效.