u/alfps

Complexity of a simple function.

Inspired by a recent posting here that was deleted (it was apparently homework) I cooked up the following simple function.

I was a little bit surprised by the result, not its general direction but how clean it was.

It should be easy to verify that I'm not a student, e.g. in its day I was a co-moderator of Usenet group comp.lang.c++.moderated. I just wonder if there is a simple, clean explanation, not heavy math-ish analysis? Explanation for not seeing the obvious: it's early morning, I still need my coffee.

auto foo( const int n ) -> int
{
    int sum = 1;
    for( int i = n - 1; i > 0; --i ) {
        sum += foo( i );
    }
    return sum;
}

#include <print>
using std::print;
auto main() -> int { print( "{}.\n", foo( 9 ) ); }
reddit.com
u/alfps — 5 hours ago

Spurious chars in terminal output from MSVC `std::print`.

I wonder if anyone can reproduce this strange problem, which happens with MSVC std::print output to Windows Terminal. System: "Microsoft Windows [Version 10.0.26200.8457]".

It does not happen when the output is redirected to a file.

It does not happen with MinGW g++.

It does not happen when std::print is replaced with printf.

#include <print>
#include <string_view>

namespace app {
    using   std::print,             // <print>
            std::string_view;       // <string_view>

    const string_view lines[] =
    {
        "                                                              ▄██▄",
        "                                                            ▄██▄▄██▄",
        "                                                          ▄██▄    ▄██▄",
        "                                                        ▄██▄▄██▄▄██▄▄██▄",
        "                                                      ▄██▄            ▄██▄",
        "                                                    ▄██▄▄██▄        ▄██▄▄██▄",
        "                                                  ▄██▄    ▄██▄    ▄██▄    ▄██▄",
        "                                                ▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄",
        "                                              ▄██▄                            ▄██▄",
        "                                            ▄██▄▄██▄                        ▄██▄▄██▄",
        "                                          ▄██▄    ▄██▄                    ▄██▄    ▄██▄",
        "                                        ▄██▄▄██▄▄██▄▄██▄                ▄██▄▄██▄▄██▄▄██▄",
        "                                      ▄██▄            ▄██▄            ▄██▄            ▄██▄",
        "                                    ▄██▄▄██▄        ▄██▄▄██▄        ▄██▄▄██▄        ▄██▄▄██▄",
        "                                  ▄██▄    ▄██▄    ▄██▄    ▄██▄    ▄██▄    ▄██▄    ▄██▄    ▄██▄",
        "                                ▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄",
        "                              ▄██▄                                                            ▄██▄",
        "                            ▄██▄▄██▄                                                        ▄██▄▄██▄",
        "                          ▄██▄    ▄██▄                                                    ▄██▄    ▄██▄",
        "                        ▄██▄▄██▄▄██▄▄██▄                                                ▄██▄▄██▄▄██▄▄██▄",
        "                      ▄██▄            ▄██▄                                            ▄██▄            ▄██▄",
        "                    ▄██▄▄██▄        ▄██▄▄██▄                                        ▄██▄▄██▄        ▄██▄▄██▄",
        "                  ▄██▄    ▄██▄    ▄██▄    ▄██▄                                    ▄██▄    ▄██▄    ▄██▄    ▄██▄",
        "                ▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄                                ▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄",
        "              ▄██▄                            ▄██▄                            ▄██▄                            ▄██▄",
        "            ▄██▄▄██▄                        ▄██▄▄██▄                        ▄██▄▄██▄                        ▄██▄▄██▄",
        "          ▄██▄    ▄██▄                    ▄██▄    ▄██▄                    ▄██▄    ▄██▄                    ▄██▄    ▄██▄",
        "        ▄██▄▄██▄▄██▄▄██▄                ▄██▄▄██▄▄██▄▄██▄                ▄██▄▄██▄▄██▄▄██▄                ▄██▄▄██▄▄██▄▄██▄",
        "      ▄██▄            ▄██▄            ▄██▄            ▄██▄            ▄██▄            ▄██▄            ▄██▄            ▄██▄",
        "    ▄██▄▄██▄        ▄██▄▄██▄        ▄██▄▄██▄        ▄██▄▄██▄        ▄██▄▄██▄        ▄██▄▄██▄        ▄██▄▄██▄        ▄██▄▄██▄",
        "  ▄██▄    ▄██▄    ▄██▄    ▄██▄    ▄██▄    ▄██▄    ▄██▄    ▄██▄    ▄██▄    ▄██▄    ▄██▄    ▄██▄    ▄██▄    ▄██▄    ▄██▄    ▄██▄",
        "▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄"
    };

    void run()
    {
        for( const string_view& sv: lines ) {
            print( "{}\n", sv );
        }
    }
}  // app

auto main() -> int { app::run(); }

Problem output (MSVC, standard output to Windows Terminal):

                                                              ▄██▄
                                                            ▄██▄▄██▄
                                                          ▄██▄    ▄██▄
                                                        ▄██▄▄██▄▄██▄▄██▄
                                                      ▄██▄            ▄██▄
                                                    ▄██▄▄██▄        ▄██▄▄██▄
                                                  ▄██▄    ▄██▄    ▄██▄    ▄██▄
                                                ▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄
                                              ▄██▄                            ▄██▄
                                            ▄██▄▄██▄                        ▄██▄▄██▄
                                          ▄██▄    ▄██▄                    ▄██▄    ▄██▄
                                        ▄██▄▄██▄▄██▄▄██▄                ▄██▄▄██▄▄██▄▄██▄
                                      ▄██▄            ▄██▄            ▄██▄            ▄██▄
                                    ▄██▄▄██▄        ▄██▄▄██▄        ▄██▄▄██▄        ▄██▄▄██▄
                                  ▄██▄    ▄██▄    ▄██▄    ▄██▄    ▄██▄    ▄██▄    ▄██▄    ▄██▄
                                ▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄
                              ▄██▄                                                            ▄██▄
                            ▄██▄▄██▄                                                        ▄██▄▄██▄
                          ▄██▄    ▄██▄                                                    ▄██▄    ▄██▄
                        ▄██▄▄██▄▄██▄▄██▄                                                ▄██▄▄██▄▄██▄▄██▄
                      ▄██▄            ▄██▄                                            ▄██▄            ▄██▄
                    ▄██▄▄██▄        ▄██▄▄██▄                                        ▄██▄▄██▄        ▄██▄▄██▄
                  ▄██▄    ▄██▄    ▄██▄    ▄██▄                                    ▄██▄    ▄██▄    ▄██▄    ▄██▄
                ▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄                                ▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄
              ▄██▄                            ▄██▄                            ▄██▄                            ▄██▄
            ▄██▄▄██▄                        ▄██▄▄██▄                        ▄██▄▄██▄                        ▄██▄▄██▄
          ▄██▄    ▄██▄                    ▄██▄    ▄██▄                    ▄██▄    ▄██▄                    ▄██▄    ▄██▄
        ▄██▄▄██▄▄██▄▄██▄                ▄██▄▄██▄▄██▄▄██▄                ▄██▄▄██▄▄██▄▄██▄                ▄██▄▄██▄▄██▄▄██▄
      ▄██▄            ▄██▄            ▄██▄            ▄██▄            ▄██▄            ▄██▄            ▄██▄            ▄██▄
    ▄██▄▄██▄        ▄██▄▄██▄        ▄██▄▄██▄        ▄██▄▄██▄        ▄██▄▄██▄        ▄██▄▄██▄        ▄██▄▄██▄        ▄██▄▄██▄
  ▄██▄    ▄██▄    ▄██▄    ▄██▄    ▄██▄    ▄██▄    ▄██▄    ▄██▄    ▄██▄    ▄██▄    ▄██▄    ▄██▄    ▄██▄    ▄██▄    ▄██▄    ▄██▄
▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄���█▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄

The code here is simplified for the posting. With the original more elaborate coding there was more spurious output (Unicode error characters), which to me sort of indicates a timing problem?


EDIT extra info:

  • There is exactly 255 bytes before the error chars, on that line.

  • The previous line only has 255 bytes, due to the spaces.

  • The problem does not happen when I use the {fmt} library's fmt::print instead of MSVC std::print.

  • The problem still does happen when I use an MSYS2 minnty terminal instead of Windows Terminal.
    However I believe also minnty uses a classic Windows console behind the scenes.

So apparently the problem is fixed size non-intelligent output chunking in MSVC's implementation of std::print.

I know of no transparent workaround, but just using old C printf is a (kludgy) workaround, at least up to 512 byte strings. I haven't tested large strings with printf.

reddit.com
u/alfps — 1 month ago