> listPtr = (int *) malloc(2 * sizeof(int));
There is no point with an initial memory allocation, you can just start with
listPtr = NULL;
realloc handles extending memory from nothing.
> listPtr = (int *) realloc(listPtr, count);
3 things wrong here
1. You're compiling with C++ - yeah, the other posters are right. In C, we don't cast the return result of malloc type calls. C++ forces you to do this. The cout calls also give the game away.
2. you forgot the sizeof(int) scaling factor
3. you don't check that the realloc call succeeded. Worse still, if it failed, you lose everything
Code:
void *temp = realloc( listPtr, count * sizeof *listPtr );
if ( temp != NULL ) {
/* success, update our real pointer */
listPtr = temp;
} else {
/* oh no, save whats in listPtr and back out */
/* save and quit, or just report can't do that */
}
> if (temp_input != -1)
do this before extending the array.
> *(listPtr + (count-1)) = temp_input;
Unless this is also an exercise in using pointer notation, its clearer to write
listPtr[count-1] = temp_input;