Search code examples
cpointerscalloc

Is the pointer calloc function return a pointer vector? (C language)


I'm trying to understand a c code, (SimpleScalar, bpred.c), there is the thing that confuses me a lot:

    int *shiftregs;
    shiftregs = calloc(1, sizeof(int));

    int l1index, l2index;
    l1index = 0; 
    l2index = shiftregs[l1index];

I delete some code that might not help. After the calloc call, *shiftregs becomes a pointer array? And what is the value of l2index? Thanks a lot!


Solution

  • Since shiftregs is a pointer to an int, *shiftregs is an int.

    Since calloc guarantees that the memory it allocates is set to 0, and you've allocated enough memory to refer to shiftregs[0], l2index will be 0 (assuming calloc didn't fail and return NULL).