0

For some reason, I am getting a segmentation fault when I try to run this test code. The program is supposed to read strings from a file and put them into an array. I'm new to C and have tried using a debugger but I am having trouble with it.

Any input would be greatly appreciated.

void fillArray(char *array[], int * count, FILE * fpin){    

char buf[40];
char *p;
count = 0;
while(fgets(buf, 40, fpin) != NULL){
    if((p= strchr(buf, '\n')) != NULL)
    *p = '\0'; //step on the '\n' 
    array[(*count)++] = malloc(strlen(buf)+1);
    assert(array[*count]);
    strcpy(array[*count], buf);
    (*count)++;
}
}
4
  • 3
    And what does it do, as opposed to what you think it should do? [Aside from the obvious typo of count = 0 that should be *count = 0, that is?] Commented Mar 14, 2013 at 20:53
  • Also, a semantic error: you're incrementing *count twice. Commented Mar 14, 2013 at 20:54
  • I already fixed that typo, posted it wrong, Sorry about that. Anyway, the code right now is giving a segmentation fault error when trying to run it in a test file. Commented Mar 14, 2013 at 20:57
  • Can you add a code how you call fillArray function? I.e. where and how you get array and other parameters? Commented Mar 14, 2013 at 22:04

2 Answers 2

1
array[(*count)++] = malloc(strlen(buf)+1);
              ^^^
assert(array[*count]);

First you increment and then use the next position in the array, presumably an uninitialized pointer. Drop the ++ from that line.

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

4 Comments

I love these clang-style error placement markers up here ^^^.
I fixed that in the code. However I still get a segmentation fault. Could it be something in my tester class main method?
@silentman45 Could be. I don't see anything else that's obviously wrong in the code you posted.
Alright, thanks for your help. I thought the error was within this piece of code, I can figure out the rest from there.
0

Hope this would help. The function automatically manages memory for array and array entries.

void fillArray(char ***array, int *count, FILE *fpin)
{
    char *tmp[] = 0;
    int tmp_allocated = 0;
    int tmp_count = 0;

    assert(array);
    assert(count);
    assert(fpin);

    while(fgets(buf, 40, fpin) != NULL)
    {
        if (( p= strchr(buf, '\n')) != NULL)
        {
            *p = 0;
        }
        if (tmp_count == tmp_allocated)
        {
            tmp_allocated += 10;
            tmp = realloc(tmp, sizeof(char*) * tmp_allocated);
            assert(tmp);
        }
        tmp[tmp_count] = strdup(buf);
        assert(tmp[tmp_count]);
        tmp_count++;
     }

     *array = realloc(tmp, sizeof(char*) * tmp_count);
     *count = tmp_count;
}

And here how we can use it.

void userOfFillArray()
{
    int count;
    char **array;
    FILE *fpin = ...;

    fillArray(&array, &count, fpin);
    // if count == 0, then array can be NULL

    ...

    while (count--)
    {
       free(array[count]);
    }
    free(array);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.