0

The following code is throwing Segmentation fault (core dumped) error when I run it. The code is compiled with g++

struct SomeClass {
    int *available;
    int **need;
    int **allocation;
}

SomeClass::SomeClass(int nR, int nT) {
    available = new int[nR];
    for (int i = 0; i < nR; i++) {
        available[i] = 1;
    }

    *allocation = new int[nT];
    *need = new int[nT];
    for (int i = 0; i < nT; i++) {
        allocation[i] = new int[nR];
        need[i] = new int[nR];
        for (int j = 0; j < nR; j++) {
            allocation[i][j] = 0;
            need[i][j] = 1; // should equal 1
        }
    }
}

Am I sure that this code is generating the error? YES! Because I commented it out and everything works fine.

I checked this question:
A segmentation fault error with 2D array

The answer says to set the stack size ulimit -s unlimited... But that didn't fix the problem.

10
  • This trows an error when you compile it or when you run it? Commented Nov 20, 2015 at 21:50
  • 1
    You are doing *allocation. Have you allocated something? Commented Nov 20, 2015 at 21:50
  • @Hogan when I run it Commented Nov 20, 2015 at 21:51
  • 1
    @rz3r0, do not do this. Period. Do not emulate 2-D array with a pointer to pointer. Just use single-dimensional arrays. Commented Nov 20, 2015 at 21:57
  • 1
    @rz3r0 I am right. Trust me. Commented Nov 20, 2015 at 22:01

2 Answers 2

3

Because your types are:

int **need;
int **allocation;

these lines:

*allocation = new int[nT]; // dereferencing uninitialized pointer
*need = new int[nT];

should be:

allocation = new int*[nT]; // proper allocation
need = new int*[nT];

Didn't you think you'd need elements of int* type for allocation[i] = new int[nR]; to work?

Sign up to request clarification or add additional context in comments.

6 Comments

*allocation = new int[nT]; is still syntactically correct, as it allocates memory for the first int* pointer in allocation (of course without having memory allocated for allocation[0] itself). But probably the OP wants what you mentioned.
@vsoftco, no it is both technically and practically incorrect.
@vsoftco -- it is a null point error since it is putting something in a place that does not exist yet.
Yes, you are right, that's why I replaced "technically correct" with "syntactically correct"
@rz3r0 I practically explained everything, do you want me to elaborate some part?
|
2

I strongly suggest (and strongly feel deja vu) to move away from an attempt to emulate 2-D arrays with pointers to pointers. It is hard to do this right. Pack all your values into single-dimensional array.

1 Comment

It's not just hard to get right, it's a silent performance killer. Sergey already knows this so I'm directing this at other readers. Rather than one contiguous memory block that is cache-friendly and predictable, a dynamic array of dynamic arrays is N+1 blocks of memory scattered across your memory space. You can't get a decent contiguous read, so the number of cache misses goes up and your program sits idle waiting on memory it could have already cached. In extreme cases, it might start thrashing, loading and unloading the same blocks over and over.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.