Frage

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!

War es hilfreich?

Lösung

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.

Andere Tipps

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
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top