Pointers, Files, and Memory Management in C

I am new to the world of C programming and at the moment I am learning about combination of pointers, pointer arithmetic with file IO and memory management. Please find my code below and this is what I am trying to do.

My program has to allocate 8 bytes of heap memory using malloc, then store a pointer from malloc to char *, and then open a file (text.txt) that contains the following lines of plain text (each 8 bytes):

chartest
chtest2!

I then try to read 8 bytes at a time from text.txt using fread until the end of the file is reached. The 8 bytes read from fread are stored in the memory chunk previously allocated with malloc. Then I use my char * to iterate over 8 bytes and print each character to stdout using printf. After every 8 bytes (and up to EOF), I reset my pointer to the 0th byte of my 8 byte block of memory and repeat to EOF.

Here is the code:

int main(void)
{
    char* array = malloc(8 * sizeof(char));

    if (array == NULL)
        return 1;

    FILE* inptr = fopen("text.txt", "r");

    if (inptr == NULL)
        return 2;

    while (!feof(inptr))
    {
        fread(array, 8 * sizeof(char), 1, inptr);

        for (int i = 0; i < 8; i++)
        {
            printf("%c", *array);
            array++;
        }
        array -= 8;
    }

    free(array);
    fclose(inptr);
    return 0;
}

      

Please note that the program was launched via valgrind, which does not report a memory leak. This is the output I am getting:

chartest
chtest2!
htest2

I don't understand where the 3rd line came from.

Also, I don't understand why when I reset my char pointer (array) using

array - = 7;

and through valgrind it reports:

SUMMARY:
== 8420 == definitely lost: 8 bytes in 1 block

Logically thinking of 8 bytes of heap memory as a character array, we would need to return a 7-place pointer to reach point 0, but this approach seems to leak memory (while an array - = 8 is fine)!

I would really appreciate it if someone could analyze this. Thank!

+3


source to share


3 answers


As pointed out in the comments, you are using it incorrectly feof

, which explains the extra line. As for subtracting 7 instead of 8: you are adding 1 to array

8 times, so why would you expect to subtract 7 to get you back to where you started?



+1


source


your file

c

h

a

r

t

e

s

t

\n

c

h

t

e

s

t

2

!

\n

the first cycle reads 8 characters and prints prints chartest

the second loop reads 8 characters and outputs \nchtest2



The third loop reads the last 2 characters and prints !\nhtest2

this because it htest2

remained in the buffer after reading to the end of the file.

checking the return value from fread () might be helpful

for example: make the following changes:

               int n = fread(array, sizeof(char), 8, inptr);

               for (int i = 0; i < n; i++)

               array -= i;

      

+1


source


I made some changes to your code and everything works fine. Here he is:

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

int main(void)
{
    char* array = malloc(9 * sizeof(char)); \\changed

    if (array == NULL)
        return 1;

    FILE* inptr = fopen("file", "r");

    if (inptr == NULL)
        return 2;

    while (!feof(inptr))
    {
        fread(array, 9 * sizeof(char), 1, inptr);  \\changed

        int i=0;
        for (i = 0; i < 8 ; i++)
        {
            if(feof(inptr)) \\added
                goto next;   \\added
            printf("%c", *array);
            array++;
        }
        printf("\n");         \\added
        next:array =array - i;    \\changed
    }    
    free(array);
    fclose(inptr);
    return 0;
}

      

You need to take care of the end-of-file and end-of-line allocated values, and for this reason your program did not work as you expected !!! EOF

\n

+1


source







All Articles