Consider following C++ snippet, which allocates memory?
int *ptr;
ptr = new int [4];Which of the following statements may make the program crash when executed subsequently?
- (A)
ptr++;
- (B)
ptr += 3;
- (C)
ptr[4] = 0x16;
- (D)
ptr = null;
Consider following C++ snippet, which allocates memory?
int *ptr;
ptr = new int [4];Which of the following statements may make the program crash when executed subsequently?
ptr++;
ptr += 3;
ptr[4] = 0x16;
ptr = null;
ANSWER
Option (c), ptr[4] = 0x16;, writes one element past the end of the 4-int block and may crash the program.
Official answer: CSource: ISRO's final answer key of 15 May 2026
new int [4] allocates a block of 4 ints, so the valid indices are ptr[0] to ptr[3], and ptr points to the first element.
(a) ptr++ moves the pointer to element 1, which is still inside the block. Only the pointer value changes and nothing is read or written, so nothing crashes.
(b) ptr += 3 moves the pointer to element 3, the last valid element. It is still inside the block, and no memory is accessed, so nothing crashes.
(c) ptr[4] is the same as *(ptr + 4), which is one element past the end of the block. Writing 0x16 there is an out-of-bounds write. It has undefined behaviour: it can corrupt the heap's bookkeeping data or touch unmapped memory, so the program may crash.
(d) null is not a C++ keyword (the null pointer is written nullptr or NULL), so as printed this is a compile-time error, not a run-time crash. Even with a valid null pointer, the assignment would only lose the address of the block, which is a memory leak and does not crash by itself.
So only (c) can make the program crash when it runs.
KEY IDEA
A block from new int [n] has valid indices 0 to n-1. Moving a pointer inside the block is harmless, but reading or writing at index n is an out-of-bounds access with undefined behaviour, and it may crash.
WATCH OUT FOR
Treating ptr[4] as the last element of a 4-element array (indices run 0 to 3), or thinking ptr = null; crashes the program. Assigning a null pointer only leaks the block, and null is not even valid C++.
From the ISRO CS 2025 paper. Every question of this paper · The full paper (PDF, ISRO) · All 2 ISRO Pointers & arrays questions, with notes