🎴 Flashcard Mode

Security Vulnerabilities and Secure Coding

Card1 / 20
Mastered0
Review0
QuestionClick to flip

In the following code snippet, how should a pointer be deleted int main (int argc, char argv[]) { char j=new char[100]; j=argv[1]; int k=atoi(j); /delete here/ return 0; }

AnswerClick to flip back
A
delete [] j
💡 Explanation:

The code allocates memory using 'new char[100]'. In C++, memory allocated with 'new[]' must be deallocated using 'delete[]'. However, the code contains a logic error where 'j' is reassigned to 'argv[1]', causing a memory leak and making 'delete[] j' dangerous.

Change Mode