1

Was trying to free memory using the for loop,

  for (int i = 0; i < sizeof(buffer); i++)
    {
        free(p[i]);
    }

but got this error

license.c:46:14: error: incompatible integer to pointer conversion passing 'char' to parameter of type 'void *' [-Werror,-Wint-conversion]
        free(p[i]);
             ^~~~
/usr/include/stdlib.h:555:25: note: passing argument to parameter '__ptr' here
extern void free (void *__ptr) __THROW;
                        ^
1 error generated.
make: *** [<builtin>: license] Error 1

this is my full code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
    // Check for command line args
    if (argc != 2)
    {
        printf("Usage: ./read infile\n");
        return 1;
    }

    // Create buffer to read into
    char buffer[7];

    // Create array to store plate numbers
    char *plates[8];

    FILE *infile = fopen(argv[1], "r");

    int idx = 0;
    char *p = 0;

    while (fread(buffer, 1, 7, infile) == 7)
    {
        // Replace '\n' with '\0'
        buffer[6] = '\0';

        p = malloc(sizeof(buffer));
        strcpy(p, buffer);

        // Save plate number in array
        plates[idx] = p;
        idx++;
    }


    for (int i = 0; i < 8; i++)
    {
        printf("%s\n", plates[i]);
    }

    for (int i = 0; i < sizeof(buffer); i++)
    {
        free(p[i]);
    }

    fclose(infile);
}
2
  • Why are you copying from buffer to p? what is that supposed to accomplish?
    – UpAndAdam
    Commented Oct 1, 2023 at 18:17
  • ahh i see you have updated it based on my feedback or perhaps others. if you found feedback useful it is suggested that you upvote it. WIll revise my answer now.
    – UpAndAdam
    Commented Oct 2, 2023 at 16:17

2 Answers 2

2

The problem is that you are freeing the wrong thing entirely and doing it incorrectly. You need to free EVERYTHING you malloc, and the things you malloc are being stored in plates, p is just one of them.

Instead of iterating over p for the sizeof(buffer) (which is incorrect because p is not a pointer to pointers, it's just a char* so should just be freed with free(p)), you need to instead iterate over plates and call free(plates[i]) for each of index of the plates array you created, which was of size 8. You are right that you need to loop over SOMETHING to do the freeing but the something is the size of the number of pointers.

0

The var "p" is the pointer. Any index (the number in brackets) is a shift index away from the pointer. The contents of "p" is an address. The contents of "p[n]" is a char, not an address. That's why you get an error. Note: C often treats chars as single bit unsigned integers.

Instead of trying to free each char with a loop, why not just...

free(p);

and free the entire char array at once? After all, that's how it was allocated! ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • Ya, free(p) should be much easier, but in this case free(p) works for only the last one, because of while cycle... so need kind of loop too. I was also trying to put free(p) inside of while loop, but then it prints something wrong. :(
    – Anna
    Commented Sep 30, 2023 at 21:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .