Consider below two program fragments.
Assume malloc is always successful, and sizes are as follows -
sizeof (int *)=8 bytes
sizeof ( double)=8 bytes
sizeof ( char )=1 byte
sizeof ( int )=4 byte
Program 1
int **p;
p = (int **)malloc( 4 * sizeof(int) );
for (int i = 0; i < 4; i++) {
p[i] = (int *)malloc( sizeof(int) );
}
Program 2
int *array = malloc(n);
for (int i=0; i<n; i+=1) array[i]=0;
Which of the following statement is correct -
- Program $1$ may produce a run time error, but Program $2$ will run fine.
- Program $2$ may produce a run time error, but Program $1$ will run fine.
- Both programs will always run fine.
- Both programs may produce run time errors.