1

Why I have a Segmentation fault in the last line? I try to search but not found the solution, can anybody tell me why I have a Segmentation fault?

int **o_position(char **map)
{
    int i;
    int j = 0;
    int k = 0;
    int count = 0;
    int **pos_o = malloc(sizeof(int *));

    for (i = 0; map[i][j] != '\0'; i++) {
        for (j = 0; map[i][j] != '\n'; j++) {
            if (map[i][j] == 'O')
                count++;
        }
    }
    pos_o = malloc(sizeof(int) * (count * 2));
    pos_o[0][k++] = count;
}
6
  • 1
    If you want a 2D array, use one. A pointer is not an array. #2345082469780923 Commented Dec 8, 2018 at 17:22
  • 1
    Your second last line, where you have a second assignment pos_o = malloc(…); throws away all the hard work done before that, leaking memory and leaving you with incomplete data structures with uninitialized pointers which lead to your crash. It's odd to have the variable k set to 0 simply so that you can eventually write pos_o[0][0] = count; (because what you've shown is equivalent to that). You don't return a value from the function, either — that's bad. Presumably, you're missing return pos_o;. And you're only allocating one int *...not an array on them. What are you trying to do? Commented Dec 8, 2018 at 17:23
  • 1
    In the outer loop ending condition for (i = 0; map[i][j] != '\0'; i++) j needs to be reset after each inner loop. Or perhaps you need map[i][0] != '\0'; Commented Dec 8, 2018 at 17:24
  • 1
    Because the second-to-last line should probably be pos_o[0] = malloc(...). Otherwise pos_o[0] is an uninitialized pointer. Commented Dec 8, 2018 at 17:27
  • @melpomene thank you so much, and the others thanks for helping me! :) Commented Dec 8, 2018 at 17:30

1 Answer 1

1

Your pos_o variable (which is a pointer to a pointer variable) stores the base-address of contiguous segment of memory for 1 unit - and here the unit is actually base address of another memory location. (And for this another memory location you are nowhere allocating memory) I think you understand this because you have already written this line int **pos_o = malloc(sizeof(int *)); - (here you are allocating 1 unit by sizeof(int*) for a pointer variable). But nowhere you have allocated memory for variable to which this pointer variable will point to.

Not allocating the memory is not a problem, but referencing the memory location without allocating the memory might result in segmentation fault.

You should have something like pos_o[0]=malloc(sizeof(int)) (I suggest allocating it for only one integer variable as I see your variable k is always 0, and you are accessing only one element). Consider allocating more (appropriate amount of) memory if you have your variable k being modified by some logic that you have not mentioned here.

Read more about segmentation faults from here.

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

Comments

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.