-2

I have still problem with a Segmentation fault in the C code. When I call the function current_live_read(ainpath); for the 8th time I'm getting the error: No source available for "_int_malloc() at 0x25be2" The main function looks like this:

void current_read(void)
{

    system(AINinit);

    char *ainpath;
    ainpath=init_current();

    int *current;
    float avgcurr=0;
    float allcurr=0;
    int i=0;

    while(1)
    {
    //sleep(1);
    i++;
    current=current_live_read(ainpath);
    allcurr=allcurr+*current;
    avgcurr=allcurr/i;
    printf("\n Current: %d AVG: %f", *current, avgcurr);
    //free(current);
    }

}

The current_live_read(ainpath); is like that:

int *current_live_read(char *ainpath)
{

    //ainpath=init_current();
    int curr;

    FILE *file = fopen(ainpath, "r");
    //free(ainpath);

    if(!file)
    {
        printf("Error opening file: %s\n", strerror(errno));
    }
    else
    {
        fscanf(file, "%4d", curr);
        fclose(file);
        //*current=curr;
    }

    free(file);


    return curr;
}

I know that something could be wrong with the pointers, but I don't know which one and what I can do about it.

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
Pukacza
  • 7
  • 1
  • 7

2 Answers2

1

You may not free the FILE * pointer after closing it. From the manpage:

Flushes a stream, and then closes the file associated with that stream. Afterwards, the function releases any buffers associated with the stream. To flush means that unwritten buffered data is written to the file, and unread buffered data is discarded.

So fclose() already does the cleaning up as needed to prevent a memory leak. If you call free() on that pointer, you are likely to corrupt your heap. So just remove the free(file);

Furthermore, you have to pass a pointer to fscanf() like this:

fscanf(file, "%4d", &curr);

Otherwise you write to a (pseudo)random memory address. It is usually a good idea to check the return value of fscanf() to see, if the conversion succeeded and handle the error case approriately.

This should eliminate the problem.

Ctx
  • 18,090
  • 24
  • 36
  • 51
  • Now smth happend with `fscanf(file, "%4d", curr);` in the `int *current_live_read(char *ainpath)` function. I have the error `No source available for "_IO_vfscanf_internal() at 0x432c4"`. Maybe the declaration of `int *curr` is wrong.; – Pukacza Sep 02 '19 at 19:05
  • @Pukacza This is indeed a second issue, see the edit – Ctx Sep 02 '19 at 19:09
  • Now the `fscanf(file, "%4d", &curr);` doesn't transfer any value to *curr. After the fscnf the value is NULL. – Pukacza Sep 02 '19 at 19:16
  • Did you check the return value of fscanf as I suggested? It should be 1, otherwise the conversion has failed due to invalid file format – Ctx Sep 02 '19 at 19:18
  • Yes it is one after `if(fscanf(file, "%4d", &curr)`. – Pukacza Sep 02 '19 at 19:23
  • @Pukacza Ok, so `curr` does contain the integer value from the file now. Note, that `*curr` is not a valid expression, because the definition is `int curr;`, so it is _not_ a pointer. – Ctx Sep 02 '19 at 19:24
  • When I change the definition to: `int *curr;` i have still the same problem. – Pukacza Sep 02 '19 at 19:26
  • @Pukacza `int curr;` is correct, I did not suggest to change this definition. – Ctx Sep 02 '19 at 19:27
  • Sorry now it return, smth didin't transfer well to my bealebone. – Pukacza Sep 02 '19 at 19:28
  • Look at my answer. I changed the definition for `int *curr;` and in `fscanf(file, "%4d", curr)` I didin't used `&curr`. – Pukacza Sep 02 '19 at 19:31
  • @Pukacza This is not correct, you have to use `int curr` and `fscanf(file, "%4d", &curr)`. Otherwise you are dereferencing an uninitialized pointer which leads to undefined behaviour (it might seem to work sometimes, but it is definitively not reliable and can crash any time) – Ctx Sep 02 '19 at 19:33
  • But when I done like that i have still segmentation fault. – Pukacza Sep 02 '19 at 19:41
  • Soo, when I have `sleep(1);` in while(1) I can get the value but without it I'm getting still Segmentation Fault. But the version that you suggest me still doesn't work even with sleep. – Pukacza Sep 02 '19 at 20:51
  • @Pukacza Use `int current;` in your `main()` instead of `int *current;` and drop the asterisk before `current` in all places. – Ctx Sep 02 '19 at 21:43
  • 1
    @Pukacza You need to learn how pointers work. You have to initialize them before you can use them. – S.S. Anne Sep 02 '19 at 22:22
  • @Ctx so when I'm using `int current;` I can't use `*current` becouse i get error `invalid type argument of unary ‘*’ (have ‘int’)` – Pukacza Sep 03 '19 at 08:46
  • So I have `int *current_live_read(char *ainpath)` as the pointer type then when I have `int current;` I will need to change `&current=current_live_read(ainpath);` but this doesn't work too – Pukacza Sep 03 '19 at 09:01
0

So I changed the int *current_live_read(char *ainpath); to int current_live_read(char *ainpath) without pointer type. Make inside function: int curr; fscanf(file, "%x", &curr) And in main function the current is just integer: int current;

Pukacza
  • 7
  • 1
  • 7