I am doing the following:
LRESURT CALLBACK WindowProc{
.......
Case VK_RETURN:
int i;
for ( i = 0; i <1000; i++) {
int size = determinesize(i);
int *pttest = new int[size];
.....(some work)....
delete[] pttest;
}
........
}
But I got problem when VK_RETURN occurs. If I don’t use delete[] pttest.
My question is that: Do I need to delete ptttest? When I search on google, almost everyone said that for each new, there must be a delete. Another question is that: if I put the for loop code for VK_RETURN in a function, say void whenvkreturn(), then do I need to delete pttest? Since any local variables are destroyed after returning, I guess the 1000 objects created in this loop will be deleted automatically, right? I don’t know the stack very well, so some explanation I found is not clear for me.
=====
OK, there is a vector workaround. But when it is the case:
TCHAR *text = new text[size];
What I am supposed to do?
You always need to delete allocated memory. For every new there should be a delete, even though it is in a function. The problem you are approaching, can easily be solved by using other containers with dynamic sizes, like std::vector. Using dynamic containers in local functions, it will automatically release the memory allocated by itself, and you don’t have to worry.