Error compiling C++ code (ld: Assertion failed) when using cout

Viewed 28

I'm trying out the counting words code from Accelerated C++ (chapter 7, pg 124–125), with the relevant code being:

int main()
{
    string s;
    map<string, int> counters;

    while (cin >> s)
        ++counters[s];

    for (map<string, int>::const_iterator it = counters.begin();
            it != counters.end(); ++it) {
        cout << it->first << "\t" << it->second << endl;
    }
    return 0;
}

However, when I try to compile this using g++-12 (Homebrew GCC 12.1.0, on macOS Monterey).

g++-12 words.cpp -o words

I obtain the following error:

0  0x1009abffa  __assert_rtn + 139
1  0x1007df28d  mach_o::relocatable::Parser<x86_64>::parse(mach_o::relocatable::ParserOptions const&) + 4989
2  0x1007cff8f  mach_o::relocatable::Parser<x86_64>::parse(unsigned char const*, unsigned long long, char const*, long, ld::File::Ordinal, mach_o::relocatable::ParserOptions const&) + 207
3  0x1008469d4  ld::tool::InputFiles::makeFile(Options::FileInfo const&, bool) + 2036
4  0x100849fa0  ___ZN2ld4tool10InputFilesC2ER7Options_block_invoke + 48
5  0x7ff81738934a  _dispatch_client_callout2 + 8
6  0x7ff81739bc45  _dispatch_apply_invoke_and_wait + 213
7  0x7ff81739b161  _dispatch_apply_with_attr_f + 1178
8  0x7ff81739b327  dispatch_apply + 45
9  0x100849e2d  ld::tool::InputFiles::InputFiles(Options&) + 669
10  0x1007bad48  main + 840
A linker snapshot was created at:
    /tmp/a.out-2022-09-19-184500.ld-snapshot
ld: Assertion failed: (_file->_atomsArrayCount == computedAtomCount && "more atoms allocated than expected"), function parse, file macho_relocatable_file.cpp, line 2061.
collect2: error: ld returned 1 exit status

I have found that removing "\t" from the code results in it working, i.e. change the cout line to:

cout << it->first << it->second << endl;

I was wondering why this is the case, and how I could get the "\t" to be output without the compilation error?

0 Answers
Related