0

So first I created a struct of an array of variable size

typedef struct arr_string {
    int size;
    char* *arr;
} arr_string;

arr_string alloc_arr_integer(int len) {
    arr_string a = {len, len > 0 ? malloc(sizeof(char*) * len) : NULL};
    return a;
}

Then I wrote a function that is meant to create a new array (called c) of strings with all the possible combinations of the strings in a given array (called a).

int alternatingSort(arr_string a) {
    int n = a.size;
    arr_string c = alloc_arr_integer(100);
    int count = 0;


    //initialize array with empty values
    for (int l = 0; l < c.size; l++){

            c.arr[l] = "";
    }

    //fill array c with all possible combinations of values in C
    for (int i = 0; i < n; i ++){
        for (int m = i+1; m < n; m ++){
            printf("hi1\n");
            char* new = strcat(a.arr[m], a.arr[i]);
            c.arr[count] = new;
            count ++;
            printf("hi2\n");
        }

    }
}

I get a bus error while running this and I'm not sure why. The first print statement "hi1" prints but not "hi2" so I believe the error is when I try to assign "new" to c.arr[count]. Did I make a mistake when allocating the memory for array c?

  • This question has been tagged with "C" and nothing else. I cannot claim I know everything there is to know about C, but I have 35 years of experience, and I have never heard of a "bus error" in C. So, would you like to explain what else you are using besides C, and show the exact error as it is displayed to you? I would be willing to bet that the message says quite a lot more than just "Bus error!" – Mike Nakis Nov 10 '19 at 00:00
  • 1
    @MikeNakis lol 'bus error' is in the same class as 'segfault' and 'Access violation':) It's common to see this hardware interrupt/exception raised on embedded systems when accessing out-of-bounds. – Martin James Nov 10 '19 at 04:24
  • Be carefull because modifying string literals is prohibited, and please post a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – MikeCAT Nov 10 '19 at 05:11
  • @MikeNakis Decades ago, when I worked on SGI Irix (MIPS CPUs), I saw two kinds of memory violations: Segmentation fault and Bus Error. I assume, Segmentation Fault you get for a valid address (concerning addressable memory) which just doesn't belong to current process. Bus Error you get for an invalid address (which just does not exist in addressable memory at all). – Scheff's Cat Nov 10 '19 at 09:01
  • @MartinJames (and Scheff) I was guessing that much, but it is a very rare error message, so people should not assume that everyone out there, or simply just the subset of people who could post helpful answers, will know any exotic term that might be thrown at them. – Mike Nakis Nov 10 '19 at 11:47
  • For what it's worth, the error is not exactly due to uninitialized pointers. It is due to array access out of bounds. It is happening because `count` is incremented more than `a.size` times, so `c.arr[count] = new;` tries to write to memory outside of the array. – Mike Nakis Nov 10 '19 at 11:50

0 Answers0