-3

I'm trying to read a file and fill an array with all the characters in the file. The problem is that in the while cycle the execution stops and there is a segmentation fault error. This is the interested function:

void allocAndFillArray(char **arrayChar, FILE *file) {
    // *file is checked before the function and 
    //  if the function is called the pointer is not NULL

    int len = x; // x is just a random size. In the real function it is the number of characters in the file
    *arrayChar = (char *)calloc(len, sizeof(char));

    int i = 0;
    while (i < len) {
        *arrayChar[i] = fgetc(file);
        i = i + 1;
    } // in this cycle the execution stops after the first iteration 
      // and only one character is written in the array 
      // before a crash reported as segmentation fault. 

   fclose(file);
}

I tried changing calloc with malloc, tried changing the size but it just doesn't work

PasqualeC
  • 3
  • 3
  • 3
    need to see [mcve]. – Sourav Ghosh Mar 23 '23 at 15:03
  • We cannot answer the question with the information you have given. I suggest you step through the code in debugger, and inspect variables as you go, to see what happens. – user694733 Mar 23 '23 at 15:08
  • You changed all your example making some comments worthless but still your example is way too short. Please read the link provided in first comment and update accordingly. – Gerhardh Mar 23 '23 at 15:22
  • What is `x`? How do you ensure `arrayChar` is not `NULL` if you don't check it in that function? Where do you check `file` for `NULL`? And why do you think the memory was not allocated? You have plenty of unchecked variables to cause the error. – Gerhardh Mar 23 '23 at 15:23
  • The bug is somewhere in some code you didn't show. We need a [mcve], otherwise there is no chance anyone can help you. – Jabberwocky Mar 23 '23 at 15:24
  • In particular it seems that calloc allocates only for 1 char and when the cycle try to insert a second character it violates some protected memory address – PasqualeC Mar 23 '23 at 15:34
  • @PasqualeC That might be what *seemingly* happens, but since standard functions are only very rarely broken, the error is much more likely to be elsewhere. Please **do** follow the link provided in the first comment, and improve your question. – DevSolar Mar 23 '23 at 15:36

1 Answers1

1

Why is calloc function not allocating the array?

You're not checking whether calloc() succeeded, and you should. But more likely than not, the answer to the title question is that calloc() in fact is allocating the array.

The problem is that in the while cycle the execution stops and there is a segmentation fault error.

That's almost surely because your function is buggy. Here ...

        *arrayChar[i] = fgetc(file);

... you appear to want this, instead:

        (*arrayChar)[i] = fgetc(file);

They do not mean the same thing. The [] operator and all other postfix operators have the highest precedence of all operators, so your original statement is equivalent to

        *(arrayChar[i]) = fgetc(file);

, which almost certainly overruns the bounds of the object designated by *arraychar if i reaches more than a very small number -- likely if it reaches even 2, though that might not be enough to trigger a segfault.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • This was the answer! I've lost an hour trying to understand what was wrong but this solved it. P.S.: Sorry if the question was not perfect, it is my first post and trying to understand how to improve – PasqualeC Mar 23 '23 at 15:41
  • We all started somewhere, @PasqualeC. I'm not sure why the question attracted so many DVs, and clearly some of the commenters were a little over-eager to dismiss the question as not containing enough information. Nevertheless, your future questions will indeed be better received if you follow our conventions, especially around presenting a MRE that demonstrates your issue. You didn't quite do that here. – John Bollinger Mar 23 '23 at 15:47