Multiple choice technology security

Will following program execute successfully ? int main(int argc,char* argv[]){ int *ptr=new int; if(ptr==NULL) exit(1); char *j; for(int i=1;i<=4;i++) { j=argv[i]; int k=atoi(j); if (k!=0){ *ptr=k; delete ptr; } } }

  1. Program works when there is only 1 argument with program

  2. Program works when there are 3 arguments with program

  3. Program works when there are 4 arguments with program

  4. Program never executes successfully

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

In the loop, delete ptr executes on first iteration (k!=0). On subsequent iterations, *ptr=k uses already-freed memory, use-after-free crash. The loop also assumes argv has 4+ elements without checking. Program will crash or have undefined behavior.