0

I have created a program that works fine but when I run it through valgrind with these flags:

-g -std=gnu11 -Wall -Wextra -Werror -Wmissing-declarations -Wmissing-prototypes -Werror-implicit-function-declaration -Wreturn-type -Wparentheses -Wunused -Wold-style-definition -Wundef -Wshadow -Wstrict-prototypes -Wswitch-default -Wunreachable-code

I get the error "invalid read of size 1". and the error occurs everytime I try to compare my char pointer array with a character. The problem begins occurring in the second function so I have included code up to that point. So in my second function everything that is compared to the storelinearray[i] is giving invalid read of size 1. What could be the problem?

 int main(int argc, char **argv){
                char* storelinearray=NULL;
                int lines = amountOfLines(argc,argv[1],&storelinearray);
                int maxcounter[7] = {0};

                countMaxWord(maxcounter, lines, storelinearray);
        }

    int amountOfLines(int argc, char argv[],char** array_with_info){
        FILE* file = NULL;
        int countLines = 0;
        char singleLine[1024];
        int i = 0;
        if(argc==2){
                file = fopen(argv, "r");

            if(file == NULL){
                perror("cant open file");
                exit(EXIT_FAILURE);
            }

            while(!feof(file)){
                countLines++;
                fgets(singleLine,1024,file);
            }
            char storelinearray[1024*countLines];
            rewind(file);

            while(!feof(file)){
                    storelinearray[i] = fgetc(file);
                    i++;
            }
            storelinearray[i] = '\0';
            *array_with_info = storelinearray;
        }

        else{
                file = stdin;
                if(file == NULL){
                perror("cant open file");
                exit(EXIT_FAILURE);
                }
                while(!feof(file)){
                    countLines++;
                    fgets(singleLine,1024,file);
                }
                char storelinearray[1024*countLines];

                rewind(file);
                while(!feof(file)){
                    storelinearray[i] = fgetc(file);
                    i++;
                }
                storelinearray[i] = '\0';
                *array_with_info = storelinearray;
        }
        fclose(file);
        return countLines;
    }

    void countMaxWord(int maxcounter[7], int lines, char* storelinearray){
        int zero = 0;
        int first = 0;
        int second = 0;
        int third = 0;
        int fourth = 0;
        int fifth = 0;
        int sixth = 0;
        int k = 0;
        for(int i = 0; i < 1024*lines;i++)
        {
            if(storelinearray[i] == '\0')
            {
                break;
            }
            if(storelinearray[i] == ':' || storelinearray[i] == '\n' )
            {
                k++;
                if(storelinearray[i] == '\n' && storelinearray[i+1] == '\n'){
                    k=0;
                }
                i++;
                    if(k>6)
                    {
                        zero = 0;
                        first = 0;
                        second = 0;
                        third = 0;
                        fourth = 0;
                        fifth = 0;
                        sixth = 0;
                        k = 0;
                    }
            }
            if(storelinearray[i] != '\0')
            {
                if(k == 0)
                {
                    zero++;
                    if(maxcounter[0] < zero){
                        maxcounter[0] = zero;
                    }

                }
                if(k == 1)
                {
                    first++;
                    if(maxcounter[1] < first){
                        maxcounter[1] = first;
                    }
                }
                if(k == 2)
                {
                    second++;
                    if(maxcounter[2] < second){
                        maxcounter[2] = second;
                    }
                }
                if(k == 3)
                {
                    third++;
                    if(maxcounter[3] < third){
                        maxcounter[3] = third;
                    }
                }
                if(k == 4)
                {   
                    fourth++;
                    if(maxcounter[4] < fourth){
                        maxcounter[4] = fourth;
                    }
                }
                if(k == 5)
                {
                    fifth++;
                    if(maxcounter[5] < fifth){
                        maxcounter[5] = fifth;
                    }
                }
                if(k == 6)
                {
                    sixth++;
                    if(maxcounter[6] < sixth){
                        maxcounter[6] = sixth;
                    }
                }
            }
        }
    }
timrau
  • 22,578
  • 4
  • 51
  • 64
kalle konsida
  • 323
  • 2
  • 5
  • 12
  • Valgrind will tell you where the problem is; use that information. Also, take a look at [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – rici Sep 24 '17 at 15:28

1 Answers1

0

In your amountOfLines(), storelinearray was declared inside the if (argc==2) {} scope. Thus, once amountOfLines() returns, it's no longer valid although you saved its address to *array_with_info, and the later functions accessed invalidated memory address.

To fix it, you need to allocate storelinearray on heap by malloc() or calloc().

timrau
  • 22,578
  • 4
  • 51
  • 64