Home > database >  Is this a perhaps a GCC issue/bug
Is this a perhaps a GCC issue/bug

Time:12-12

While I was solving today's AOC challenge I stumbled upon an interesting phenomenon where it appears that there is a problem with the GCC compiler or I have done something incorrect which I have yet to realize.

In the below code, I am parsing a list of numbers offset on a line read from a file input.txt:

  Starting items: 83, 62, 93

Something appears to go wrong when storing the result in the num variable. I have yet to check the disassembly to see what is going on.

#include "stdio.h"
#include "stdlib.h"
#include "../../includes/fmt.c"

void problem();

int main() {

    FILE * fp = fopen("input.txt", "r");
    char * line = NULL;
    size_t len = 0;
    
    size_t length = (getline(&line, &len, fp) - 16) >> 2;
    problem(line, length);
}

void problem(char * line, size_t length) {
    for (size_t i = 0; i < length;   i) {
        int num = 10 * (line[18   (i << 2)]-'0')
                       (line[19   (1 << 2)]-'0');
        println("{2i: = }", num, 10 * (line[18   (i << 2)]-'0')
                                      (line[19   (i << 2)]-'0'));
    }
}

Output:

82 = 83
62 = 62
92 = 93

I know that the error is not in my custom print function as I have tried without it.

So my question is. Is this a fault of my own or perhaps an error of the GCC?

Linux 5.15.82-1-lts x86_64 GCC 12.2.0

CodePudding user response:

It looks like there may be an error in the code on the line

int num = 10 * (line[18   (i << 2)]-'0')   (line[19   (1 << 2)]-'0');

The index of the second character being read appears to be incorrect. Instead of line[19 (1 << 2)], it should be line[19 (i << 2)] to access the correct character in the string.

  • Related