0

I have a struct:

typedef struct{
        int *issueTypeCount;
    }issueTypeTracker;

I've declared a variable of type issueTypeTracker:

issueTypeTracker *typeTracker;

I've allocated necessary memory:

typeTracker = (issueTypeTracker*) malloc(sizeof(issueTypeTracker) * issueTypeList.count());
typeTracker->issueTypeCount = (int*) calloc(65536,sizeof(int));

And then when I try to do something with it, I get a segmentation fault

while(qry.next()){ //while there are records in the query
        for(j=0;j<locationList.count();j++){ // no problem
            if(qry.value(1) == locationList[j]){ //no problem
                for(i=0;i<issueTypeList.count();i++){ //no problem
                    typeTracker[j].issueTypeCount[i]++; //seg fault as soon as we hit this line
                }
            }
        }
    }

I figured it would be a problem with the way i've allocated memory, but as far as I'm aware i've done it correctly. I've tried the solutions proposed in this question, however it still did not work.

I've tried replacing typeTracker->issueTypeCount = (int*) calloc(65536,sizeof(int)); with:

for(j=0;j<issueTypeList.count();j++){
        typeTracker[j].issueTypeCount = (int*) calloc(65536,sizeof(int));
    }

But I still get the same issue. This happens with any value of j or i, even zero.

This is a lot more trouble than it's worth and a poor implementation of what I'm trying to do anyway, so I'm probably going to scrap this entire thing and just use a multidimensional array. Even so, I'd like to know why this doesn't work, so in the future I don't have trouble when i'm faced with a similar scenario.

14
  • 1
    Check the return value of malloc/calloc. Commented Jun 13, 2014 at 16:10
  • @bitmask I ran it through the debugger again - as expected, typeTracker's value is a memory address (different from the one it starts with so I know malloc worked) and typeTracker.issueTypeCount's value is zero. Commented Jun 13, 2014 at 16:14
  • 1
    Any reason it's not just a std::vector<int> and a std::vector<issueTypeTracker>? Commented Jun 13, 2014 at 16:23
  • 1
    @Lighthat: There you have your answer :) --- If typeTracker.issueTypeCount is the nullptr then this means calloc refused to answer your request for memory (for whatever reason I do not dare to speculate). Naturally, accessing this means accessing memory that doesn't belong to you. Hence a segfault. Commented Jun 13, 2014 at 16:25
  • 2
    You allocate an array of issueTypeTracker (with malloc) then you only initialize the first element (with calloc). Commented Jun 13, 2014 at 16:29

1 Answer 1

1

You have several issues. Firstly, you're not checking your allocations for success, so any of your pointers could be NULL/nullptr.

Secondly,

typeTracker->issueTypeCount = (int*) calloc(65536,sizeof(int));

is equivalent to

typeTracker[0].issueTypeCount = (int*) calloc(65536,sizeof(int));

so, you initialized the issueTypeCount member for only the first issueTypeTracker in your array. For the other issueTypeList.count() - 1 elements in the array, the pointer is uninitialized.

Therefore this line:

typeTracker[j].issueTypeCount[i]++; //seg fault as soon as we hit this line

will invoke UB for any j>0. Obviously if your allocation failed, you have UB for j==0 as well.

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

3 Comments

For the first issue, I'm fairly certain that the allocations are succeeding because i'm getting memory addresses where I expect them and zero values where I expect them (the actual data contained in the array). Either way you are correct and it's an easy fix. For the second issue, I caught that too and that's why I used the for loop approach, but I still get the segmentation fault for any value of i or j.
Don't be fairly certain, just code it correctly so it can't be wrong. Check the return value and bail out if an allocation fails. When the process cores, what was the address it was trying to de-reference? What were the values of i and j? If you still can't see how you got to that point, run under valgrind and see if something else is damaging your array.
The values of i and j are within bounds of both arrays. The address being dereferenced is not 0x0, it's a valid memory address. I am using the Qt debugger and nothing seems out of the ordinary at all, which is why this problem stumps me. I know I should check the return values of the memory allocation in the program, but right now I am using the debugger and the memory is being allocated properly in this instance...except, apparently not, since I'm getting a segfault. I am certain I'm not accessing a null pointer.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.