문제

I'm new to C++ (porting from Java), and I cannot understand whether or not I have a memory leak in my code.

this is the basis of my code (it's far more complicated, I bring the important places where memory can leak)

char message[15000];
char allMessages[102400];
int allMessagesCounter;

int main() {
    connect() \\this works just fine
    openThreadAndGetAllMessages() \\ here I get each time a message to my message char array and concatenate it into allMessages array using allMessagesCounter
    cout << allMessages;
    disconnect() \\works just fine as well
}

My question is, do I need to free the message[] and allMessages[]?

Thanks!

도움이 되었습니까?

해결책

No. In general, in C++, if you use the keyword new then you should also delete later. Your variables are allocated statically, and do not need to be deleted.

다른 팁

No you do not because they are not dynamically allocated. You only need to call delete if memory is allocated with new:

 int *a = new int [5]; //must call delete [] or memory leak
 int b [5]; //no need
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top