I wanted to expand an array dynamically as the user enters the values using below code.
#include<stdio.h>
#include<stdlib.h>
int main(){
int *number=(int *)malloc(sizeof(int)),i=0,*new_ptr;
printf("Enter a number to store it or -1 to finish the storage\n");
scanf("%d",number);
while(*(number+i)!=-1){
printf("Enter a number to store it or -1 to finish the storage:\n");
number=(int *)realloc(number,sizeof(number)+sizeof(int));
i++;
scanf("%d",number+i);
};
for(int j=0;j<i;j++){
printf("%d ",number[j]);
};
return 0;
}
it gives this output
Enter a number to store it or -1 to finish the storage
1
Enter a number to store it or -1 to finish the storage:
2
Enter a number to store it or -1 to finish the storage:
3
Enter a number to store it or -1 to finish the storage:
4
Enter a number to store it or -1 to finish the storage:
5
Enter a number to store it or -1 to finish the storage:
6
Enter a number to store it or -1 to finish the storage:
7
Enter a number to store it or -1 to finish the storage:
8
Enter a number to store it or -1 to finish the storage:
9
Enter a number to store it or -1 to finish the storage:
0
Enter a number to store it or -1 to finish the storage:
-1
1 2 3 4 5 6 7 8 540155953 540287027
the junk file values happens only when the number of user input is greater than 8. Why it happens and how to fix it?
There were a few small issues with the original code. Junk values got printed because the original code invoked undefined behavior. Specifically, realloc() was not allocating enough space in the old logic. realloc() needs to know new total space.
Original code was always realloc()ing sizeof(number)+sizeof(int)... which translates to sizeof(int *) + sizeof(int).
That combo results in a constant which does not grow. This may at least partly explain why you could get OK performance until the array actually needed more space than sizeof(int *) + sizeof(int). The new code fixes that by updating the allocation size logic.
Other adds/updates: