0

I'm trying to submit code that is auto graded for uni, and I fail the last test case checking for memory being cleared and other errors. I have been looking at my code for hours and have no idea why it outputs errors

in use at exit: 0 bytes in 0 blocks
ERROR SUMMARY: 11 errors from 3 contexts (suppressed: 0 from 0)

This code is supposed to take either a file or stdin, and print out each line in reverse. It does that correctly without crashing. It even passes all the functional test cases.

#define _DEFAULT_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>


int main(int argc, char ** argv){
    FILE* file;
    if(argc>1){
        file = fopen(argv[1],"r");
    }
    else{
        file=stdin;
    }
    //FILE* file = fopen(argv[1],"r");
    //if(!(file))file=stdin;

    
    int len=0;
    char **array = malloc(sizeof(char*));
    array[len] = malloc(sizeof(char)*255);
    
    while(!(feof(file))){               
        fgets(array[len],255,file);
        len++;
        array = realloc(array,sizeof(char*)*(len+1));
        array[len] = malloc(sizeof(char)*255);
    }
    

    for(int i=len;i>=0;i--){
        printf("%s",array[i]);
    }
    
    for(int i=0; i<=len; i++){
        free(array[i]);
    }
    
    fclose(file);
    free(array);
    
    return 0;
}

Any help would be appreciated thanks!

NickVHoy
  • 3
  • 1
  • Welcome to Stack Overflow! [Why `while(!feof(file))` is always wrong](https://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) – Barmar Mar 02 '22 at 22:26
  • in general asking SO to explain the errors that valgrind found without adding the errors to the question is not helpful. – pm100 Mar 02 '22 at 22:29
  • @pm100 this is run on an online code server with files I do not have access to, I 100% agree it is a lot less helpful, my prof basically said "wow that's crazy" when I asked him this question. – NickVHoy Mar 02 '22 at 22:38
  • @Barmar Thanks am giving that a read now! – NickVHoy Mar 02 '22 at 22:38
  • 1
    If the remote code scoring script doesn't give you enough information, you can run valgrind yourself. Maybe some valgrind options would matter, but I think it describes all "errors" by default. – aschepler Mar 02 '22 at 22:52
  • @aschepler I would not have access to the files in the code server that are passed to my code, however I guess I could make my own, I don't have valgrind setup at all either and I think its linux only? but yeah would prefer to do that, got it figured out though. – NickVHoy Mar 02 '22 at 22:58

1 Answers1

0

You are not null terminating the last line

while (!(feof(file))) {
    fgets(array[len], 255, file);
    len++;
    array = realloc(array, sizeof(char*) * (len + 1));
    array[len] = malloc(sizeof(char) * 255);
    *array[len] = 0; <<<<==========
   }

This at least produces good output now, dont knwo if valgrind complains or not because I am not on linux.

pm100
  • 48,078
  • 23
  • 82
  • 145
  • Thanks so freaking much, literally have been looking at this for 12 hours this week wondering what was broke! – NickVHoy Mar 02 '22 at 22:45
  • 1
    The real problem is that `len` ends up one too large because of the `while (!feof(file))` mistake. This works around that by putting an extra useless empty string `"\0"` at the end, and printing that has no effect. – aschepler Mar 02 '22 at 23:07
  • @aschepler yup. – pm100 Mar 02 '22 at 23:16