释放c字符串数组

时间:2016-11-29 20:37:26

标签: c++ c-strings

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

  • 释放这样一个阵列的正确方法是什么?

  • 有必要单独释放A的每个元素,如下面的代码?

谢谢。

#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;
}

1 个答案:

答案 0 :(得分:6)

是。只要每个import time import threading import pyHook import pythoncom def DoThis(Cond): while True: with Cond: #calling "with" automatically calls acquire() on the lock print(time.time()) print('stopping...') global Cond Cond = threading.Lock()#create a threading lock Cond.acquire() #give the lock to the main thread global t1 t1 = threading.Thread(target=DoThis,args=(Cond, )) #initialize the thread that does stuff while mouse button is down t1.start() #start the thread, it won't do anything until mouse button is down def OnDown(go): global Cond Cond.release() #allow the thread to acquire the lock print('Lock released') return True def OnUp(go): global Cond Cond.acquire() #take the lock away from the thread print('Lock acquired') return True hm = pyHook.HookManager() hm.MouseLeftDown = OnDown hm.MouseLeftUp = OnUp hm.HookMouse() pythoncom.PumpMessages() new ...[]匹配,一切正常。