Process finished with exit code 11 | Error during malloc [duplicate]

Trying to understand memory allocation in C. Facing issue while trying to create two arrays using pointer to integers. Kindly have a look at the below code:

#include <stdio.h>
#include <stdlib.h>

int main() {

    int *a;
    int *b;

    for (int i = 0; i<4;i++)
    {
        printf("Enter value \n");
        a[i]=(int *)malloc(sizeof(int));
        b[i]=(int *)malloc(sizeof(int));
        scanf("%d",&a[i]);
        scanf("%d",&b[i]);
    }
    for (int i =0;i<4;i++)
    {
      printf("%d = %x\n  ",a[i],&a[i]);

    }
    for (int i =0;i<4;i++)
    {
     printf("%d = %x\n  ",b[i],&b[i]);

    }

    return 0;
}

I am working with C11 on CLion. Facing below error on runtime. Can someone please explain what is wrong with this code ?

Enter value 

Process finished with exit code 11

“b is being shown NULL during debugging”

UPDATE: Tried on another IDE, where “a” itself is not being allocated any memory. It directly gives me segmentation fault.

UPDATE 2: Changing:

int *a;
int *b;

to

int *a = NULL;
int *b = NULL;

at least stops how this code is behaving. It gives me segmentation fault as soon as I try to allocate memory to a[i] (Which is wrong, now I get).

Leave a Comment