释放 C 字符串数组

Freeing array of c-strings

这里我有一组动态分配的 C 字符串。

谢谢。

#include <string.h>
int main()
{
    const char** A = new const char*[3];
    A[0] = (const char*)memcpy(new char[5], "str0", 5);
    A[1] = (const char*)memcpy(new char[5], "str1", 5);
    A[2] = (const char*)memcpy(new char[5], "str2", 5);

    // Is this the proper way to free everything?
    for (int i = 0; i < 3; ++i)
        delete[] A[i];
    delete[] A;
}

是的。只要每个 new ...[] 都与 delete[] ....

匹配,一切都可以