1

So I was running a simple C++ code containing this piece

freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
for(int k=i;k--;k>=1)
{
    if(outgoing[k]=='Y' && incoming[k-1]=='Y')
        result[i][k-1]='Y';
    else
        break;
}

So usually the error if there is any it printed into console output but while running this code it printed this error inside the output file

/home/keith/builds/mingw/gcc-9.2.0-mingw32-cross-native/mingw32/libstdc++-v3/include/bits/basic_string.h:1067:
 std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::reference
 std::__cxx11::basic_string<_CharT, _Traits,
 _Alloc>::operator[](std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>;
 std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::reference =
 char&; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type
 = unsigned int]: Assertion '__pos <= size()' failed.

I don't understand why? And who is Keith?

I know the error by the way here for(int k=i;k--;k>=1) k-- and k>=1 should be swapped

Saurav
  • 614
  • 7
  • 13
  • The assertion says you gave it a bad index to `operator[]`. Make sure `k` is a valid index for the string you are accesing, i.e., `k < string.size()` – NathanOliver Jul 27 '20 at 13:39
  • 1
    Seems that you are accessing Keith's compiler. – john Jul 27 '20 at 13:43
  • 1
    @john I am not accessing Keith's computer – Saurav Jul 27 '20 at 13:48
  • @NathanOliver so I think as while checking first time k=0 and when it processes k-- so value becomes k=-1 and its still will result in running the loop. So It will be a bad index . Thanks – Saurav Jul 27 '20 at 13:48
  • But still why the output is redirected to output.txt file? And is mingw contains this "Keith" keyword? – Saurav Jul 27 '20 at 13:50
  • 1
    @SauravKumarSingh Well I said compiler not computer. – john Jul 27 '20 at 13:52
  • 1
    @SauravKumarSingh Uh, you ask to redirect output to file here: `freopen("output.txt", "w", stdout);`. And that's just a filepath, apparently your MinGW is installed in `/home/keith`. If you have multiple installations on your computer (like one for you and one for Keith), you may need to point to correct one, but that's a different question (it's related to your build system, not to C++ code itself). – Yksisarvinen Jul 27 '20 at 13:53
  • _"And who is Keith?"_ – Asteroids With Wings Jul 27 '20 at 14:12
  • Did you build MinGW from source, or did Keith build it for you (and you just installed the binaries that Keith made)? (That's probably Keith Marshall.) – Eljay Jul 27 '20 at 14:13
  • @Yksisarvinen This info _should_ go to stderr... – Asteroids With Wings Jul 27 '20 at 14:15
  • Provide a [mcve] including build and execution commands. – Asteroids With Wings Jul 27 '20 at 14:16
  • Keith is not the username I use or anyone use on my computer. And my only question is why this error is reproduced to the output file as i mentioned clearly `stdout` in the code – Saurav Jul 27 '20 at 14:30
  • Are you mixing cout and stdout? see https://stackoverflow.com/q/1924530/2785528 – 2785528 Jul 27 '20 at 15:57
  • @2785528 No mixing. – Saurav Jul 27 '20 at 16:34
  • 1
    "keith" was the username under which the C++ standard library that you are using was built. The library code likely uses `assert` macro or similar, which in turn uses `__FILE__`, which captured the file name as it existed on the build machine at build time. – Igor Tandetnik Jul 31 '20 at 15:24

1 Answers1

0

Whenever you are reading characters from a string or file, you have to make sure your loop ends before the string or file reaches \n or EOF.

This can be verified by the compiler only if you mention bounds for the string or file which is known to compiler.

So instead of running the loop until k>=1 maybe you can use a while loop or maybe str.length() where str is a string variable.

And maybe Keith is someone who was involved in writing the mingw compiler.

Niresh
  • 67
  • 7