Need help on C programming -> pointer

In the statement

*pointer++ = j;

the pointer is moved to next location in the allocated memory. [The post increment operator increase the value of operand by 1 but the value of the expression is the operand’s original value prior to the increment operation]

After for loop the pointer is pointing to memory one past the allocated array memory and here

delete [] pointer;

your program is trying to delete a memory which it doesn’t own.

To fix this, while inserting the elements in the array take a temporary pointer

int* temp = pointer;
for (int j = 0; j < 1000; j++)
    *temp++ = j;

Leave a Comment