0
int* dynamicArray(int n, int queries_rows, int queries_columns, int** queries, int* result_count) {
int i,j;
int lastAnswer = 0,y,resultCount = 0;
int *result = NULL;
int **seqList = (int**) calloc (n,sizeof(int*));
for (i=0; i<queries_rows;i++)
{
    y = (queries[i][1] ^ lastAnswer)% n;
    if(queries[i][0] == 1){
        if(seqList[y]==NULL){
            int *dummy = (int*) calloc (2,sizeof(int));
            seqList[y]=dummy;
        }
        for(j=0;j<n;j++){
            if(seqList[y][j])
                continue;
            else {
                printf("%d %d entry %d",y,j,seqList[y][j]);
                seqList[y][j] = queries[i][2];
            }
        }
    }
    if(queries[i][0] == 2){
        lastAnswer = seqList[y][queries[i][2]];
        resultCount++;
        if(result == NULL)
            result = (int*) calloc (1,sizeof(int));
        else {
            result = (int*) realloc (result,resultCount * sizeof(int));
        }
        result[resultCount - 1] = lastAnswer;
    }
}
*result_count = resultCount;
return result;

}

Anything wrong with the above realloc usage for giving out a "segfault"? Is this the right way to use realloc? Also running a debugger is not possible as this is a function completion of cooding site?

  • 2
    '"running a debugger is not possible as this is a function completion of cooding site". Doesn't seem like a good excuse. Just run the same code on your PC and debug it there. – kaylum Jul 01 '20 at 04:32
  • 1
    If you compile the code on your PC it will immediately give you warnings that will point you straight to the major problems. `resultCount` is a pointer yet you use it like an `int` in many places. Like `resultCount * sizeof(int)` should be `*resultCount * sizeof(int)` and `result[resultCount - 1] = lastAnswer;` should be `result[*resultCount - 1] = lastAnswer;` – kaylum Jul 01 '20 at 04:34
  • resultCount is an integer only and I believe you are talking about result_count, so I believ that part works fine. – Abhishek Sa Jul 01 '20 at 05:01
  • 1
    Sorry you are right. Though it's not good practice to have variable names that are so similar. Anyway, it is difficult to spot your error just from inspection. You really need to hit it with a debugger. It will tell you immediately which line of code triggers the seg fault and you can examine the indices as there is a good chance one of them is wrong. – kaylum Jul 01 '20 at 05:10
  • How big is `n`? – Bob__ Jul 01 '20 at 14:16
  • @Bob__: For sample basis 1 99638520 173318136 1 854060080 407068012 2 980658213 778573744 2 412539660 476853104 //But all of the above lines are within the range of int. n will be till 10^5 – Abhishek Sa Jul 01 '20 at 17:14
  • @kaylum: Will try to reproduce it. Thanks. – Abhishek Sa Jul 01 '20 at 17:17

2 Answers2

0

You are missing a few spots to "derive" a pointer to do any sort of action with the value...so you're probably trying to allocate/reallocate dynamic memory with the integers memory address instead of the value of the pointer(which would be deriving it).
Try putting an asterisk in front of the pointer variables when allocating/reallocating the memory.

  • resultCount is an integer only and I believe you are talking about result_count, so I believ that part works fine. **It can be easily missed** – Abhishek Sa Jul 01 '20 at 17:13
0

Note these lines

int **seqList = (int**) calloc (n,sizeof(int*));
for (i=0; i<queries_rows;i++)
{
    y = (queries[i][1] ^ lastAnswer)% n;  // <-- ?

    if(queries[i][0] == 1) {
        if(seqList[y]==NULL) {
            int *dummy = (int*) calloc (2, sizeof(int));
            //                         ^^^
            seqList[y]=dummy;
            //     ^^^
        }
        for( j = 0; j < n; j++ ) {
        //          ^^^^^               is n bigger than 2? 
        if( seqList[y][j] )
        //            ^^^
            continue;
        else {
            // ...
            seqList[y][j] = queries[i][2];
            //        ^^^
        }
    }
}

According to what the OP commented "n will be till 10^5", but only enough memory to store a couple of ints has been allocated.

Also note that both calloc and realloc may fail, but none of the values returned by those functions are checked.

Further references to address other issues

Bob__
  • 12,361
  • 3
  • 28
  • 42