Xcode 14 update has bugs and my code in c++ is not compiling with memory issues.

Earlier my C++ was working on Xcode 13 versions but when updated to Xcode 14 it is now returning various errors (seems memory errors). The same code is compiling on other platforms such as windows and in online compilers.

Error:-

0  0x1029141a0  __assert_rtn + 140
1  0x10279ba8c  mach_o::relocatable::Parser<arm64>::parse(mach_o::relocatable::ParserOptions const&) + 4536
2  0x10276dd38  mach_o::relocatable::Parser<arm64>::parse(unsigned char const*, unsigned long long, char const*, long, ld::File::Ordinal, mach_o::relocatable::ParserOptions const&) + 148
3  0x1027d64ac  ld::tool::InputFiles::makeFile(Options::FileInfo const&, bool) + 1468
4  0x1027d9360  ___ZN2ld4tool10InputFilesC2ER7Options_block_invoke + 56
5  0x188b381f4  _dispatch_client_callout2 + 20
6  0x188b4b954  _dispatch_apply_invoke + 224
7  0x188b381b4  _dispatch_client_callout + 20
8  0x188b49a04  _dispatch_root_queue_drain + 680
9  0x188b4a104  _dispatch_worker_thread2 + 164
10  0x188cf8324  _pthread_wqthread + 228
A linker snapshot was created at:
	/tmp/solution-2022-09-14-105028.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

Code :


#include<bits/stdc++.h>
using namespace std;

#define     int             long long int
#define     vi              vector<int>
#define     pii             pair<int,int>
#define     fast()          ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define     all(x)          begin(x),end(x)
#define     rep(x,a,b)      for(int x = a;x<(b+1);x++)
#define     F               first
#define     S               second

const long long inf = 1e18, mod = 1e9 + 7;

int pw(int x, int y, int p = mod) {
    if (y == 0) return 1;
    if (y == 1) return x;
    int res = pw(x, y / 2);
    if (y & 1) return ((res * res) % p * x) % p;
    return (res * res) % p;
}

int nu(vi & suff, int l, int r){
    int n = suff.size();
    int ans = suff[l] - ((r<n-1)?suff[r+1]:0);
    int di = pw(10, n-r-1);
    ans = (ans*pw(di, mod-2))%mod;
    return ans;
}

void test_case()
{
    string s; cin>>s;
    int n = s.size();
    int w,m; cin>>w>>m;
    vi suff(n+1,0); suff[n] = s[n-1]-'0';
    for(int i = n-2;i>=0;i--){
        suff[i+1] = ((s[i]-'0')*pw(10,n-i-1) + suff[i+2])%mod;
    }
    map<int, pii> mp;
    for(int i = n-w+1;i>0;i--){
        int t = nu(suff, i, i+w-1)%9;
        mp[t] = {i, (mp.count(t)>0?mp[t].F:0)};
    }
    while(m--){
        int l,r,k; cin>>l>>r>>k;
        int ans1 = inf, ans2 = inf;
        rep(j,0,8){
            int l1 = mp[j].F;
            int v = nu(suff, l, r);
            int p = (j*v)%9;
            int x = (k-p+9)%9;
            int l2 = mp[x].F;
            if(l2==l1) l2 = mp[x].S;
            if(l1>0 and l2>0){
                if(l1<ans1){
                    ans1 = l1;
                    ans2 = l2;
                }else if(l1==ans1){
                    ans2 = min(ans2, l2);
                }
            }
        }
        if(ans1<inf and ans2<inf){
            cout<<ans1<<" "<<ans2<<endl;
        }
        else cout<<"-1 -1"<<endl;
    }

}
int32_t main() {
    fast();
    int tt = 1;
    cin >> tt;
    for(int i = 1;i<=tt;i++) {
        // cout<<"Case #"<<i<<": ";
        test_case();
    }
    return 0;
}

Give input to code:

5
1003004
4 1
1 2 1
179572007
4 2
2 7 3
2 7 4
111
2 1
2 2 6
0000
1 2
1 4 0
1 4 1
484
1 5
2 2 0
2 3 7
1 2 5
3 3 8
2 2 6
Post not yet marked as solved Up vote post of fanick Down vote post of fanick
7.3k views
  • If you are trying to run this code and #include<bits/stdc++.h> is not working on your system, replace it with

    #include<iostream> #include<vector> #include<map>

    Then you check for compilation issue.

Add a Comment

Replies

This builds just fine for me. Specifically:

  1. On macOS 12.5.1, I ran Xcode 14.0.

  2. I created a new project from the macOS > Command Line Tool template, selecting C++ as the language.

  3. I replaced main.cpp with your code.

  4. I replaced the use of <bits/stdc++.h>, per your comment.

  5. I choose Product > Build and it built as expected.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

  • Please update your macOS to 12.6 and then try this code. Thank you.

  • P.S. I have tried this code in Sublime Text editor and VS code.

Add a Comment

@eskimo Please update your macOS and try again.

#include<bits/stdc++.h>
#define     int             long long int

Those two lines are ..... unconventional??!

Does it still fail if you instead #include specific headers for vector, iostreams etc. and use a non-reserved-word (e.g. "Int") for your long long int macro?

How exactly are you compiling it?

Edited to add:

After you've changed your #define to Int not int, change the signature of main back to int main.

  • @endecotp Those two lines have nothing to do with this issue as I have been using them since years. It was compiling perfectly before Xcode update. Though, you can change these lines:- first line as I mentioned above and you can completely remove the second line. This code works only in Xcode but fails everywhere else, I tried in terminal also. To compile:-save this code in file with name solution.cpp, then go to that directory and run the command: "g++ --std=c++17 solution.cpp" Check below code

Add a Comment

I have tried different codes, they are also not working (not all codes have this issue). I confirmed it with my friends and they are also getting the same problem after update.

Here is the simplified version of above code with the same issue:-


#include<iostream>
#include<vector>
#include<map>
using namespace std;

const int inf = 2e9;

int nu(vector<int>& suff, int l, int r, int n){
    int ans = suff[l] - (r<n?suff[r+1]:0);
    return ans%9;
}

void test_case()
{
    string s; cin>>s;
    int n = s.size();
    int w, m; cin>>w>>m;
    vector<int> suff(n+1,0); suff[n] = (s[n-1]-'0');
    for(int i = n-2;i>=0;i--){
        suff[i+1] = suff[i+2] + (s[i]-'0');
    }
    map<int, pair<int, int>> mp;
    for(int i = n-w+1;i>0;i--){
        int t= nu(suff, i, i+w-1, n);
        mp[t] = {i, mp[t].first};
    }
    while(m--){
        int l,r,k; cin>>l>>r>>k;
        int v = nu(suff, l, r, n);
        int ans1 = inf, ans2 = inf;
        for(int j = 0;j<9;j++){
            int l1 = mp[j].first;
            int p = (j*v)%9;
            int x = (k-p+9)%9;
            int l2 = mp[x].first;
            if(l2==l1) l2 = mp[x].second;
            if(l1>0 and l2>0){
                if(l1<ans1){
                    ans1 = l1;
                    ans2 = l2;
                }else if(l1==ans1){
                    ans2 = min(ans2, l2);
                }
            }
        }
        if(ans1<inf and ans2<inf){
            cout<<ans1<<" "<<ans2<<endl;
        }else cout<<"-1 -1"<<endl;
    }
}
int main() {
    int tt = 1;
    cin >> tt;
    for(int i = 1;i<=tt;i++) {
        test_case();
    }
    return 0;
}

Please update your macOS to 12.6 and then try this code.

The macOS version is unlikely to be an issue here. Xcode uses the macOS SDK that’s built in to it; it has no dependency on the macOS version except when it comes to actually running the code.

P.S. I have tried this code in Sublime Text editor and VS code.

I recommend that you test this with Xcode, just to establish a baseline.


endecotp wrote:

Those two lines are ..... unconventional??!

Hey hey! Back in the day I was debugging a problem and found this in the code:

#define void int

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Here is the simplified version of above code with the same issue:-

Works for me on macOS 12.5, ARM, Xcode 14.0.0. Sorry, I'm not going to install the 12.6 beta just to check this!!! Remind me when it is released and I'll try again.

Those two lines have nothing to do with this issue as I have been using them since years.

Indeed they are probably nothing to do with the issue, but they're still very unconventional and you should not use them. The fact that something has been working for years doesn't mean that it's right, by any means!

I would also suggest that you turn on warnings (-W and possibly -Wall). They don't report anything in this case, but it's still good practice.

  • @endecotp Please check the newer version of the code which I pasted above. It is free of all those things you pointed out. Please check it anywhere other than Xcode. Open terminal and you can compile it using command:- "g++ --std=c++17 filename.cpp".

Add a Comment

@endecotp Please compile the newer version of code anywhere other than Xcode. MacOS version does not matter. XCode version should be 14.0.0.

@fanick I am facing exactly same issue how do I resolve it please?

  • I too am facing this issue, can someone provide some solution?

Add a Comment

Please compile the newer version of code anywhere other than Xcode. MacOS version does not matter. XCode version should be 14.0.0.

That's what I did; I copied&pasted the code in your reply "here is the simplified version of the code" above into a file, and compiled on the command line with the g++ invocation exactly as you indicated in an earlier comment. It compiles without error.

Note again that I'm using macOS 12.5 on ARM.

My guess is that there is something odd in your environment. For example, since you're the sort of person who is happy to #define int :-) , do you have any shell aliases set up? Is g++ actually an alias for something else?

Suggestion for how to debug this: run g++ --verbose and compare with what I get:

% g++ --verbose --std=c++17 -W -Wall solution.cpp 
Apple clang version 14.0.0 (clang-1400.0.29.102)
Target: arm64-apple-darwin21.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple arm64-apple-macosx12.0.0 -Wundef-prefix=TARGET_OS_ -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -Werror=implicit-function-declaration -emit-obj -mrelax-all --mrelax-relocations -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name solution.cpp -mrelocation-model pic -pic-level 2 -mframe-pointer=non-leaf -fno-strict-return -fno-rounding-math -funwind-tables=2 -fobjc-msgsend-selector-stubs -target-sdk-version=12.3 -fvisibility-inlines-hidden-static-local-var -target-cpu apple-m1 -target-feature +v8.5a -target-feature +fp-armv8 -target-feature +neon -target-feature +crc -target-feature +crypto -target-feature +dotprod -target-feature +fp16fml -target-feature +ras -target-feature +lse -target-feature +rdm -target-feature +rcpc -target-feature +zcm -target-feature +zcz -target-feature +fullfp16 -target-feature +sm4 -target-feature +sha3 -target-feature +sha2 -target-feature +aes -target-abi darwinpcs -fallow-half-arguments-and-returns -debugger-tuning=lldb -target-linker-version 819.6 -v -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/14.0.0 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -I/usr/local/include -stdlib=libc++ -internal-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1 -internal-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/local/include -internal-isystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/14.0.0/include -internal-externc-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -internal-externc-isystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include -W -Wall -Wno-reorder-init-list -Wno-implicit-int-float-conversion -Wno-c99-designator -Wno-final-dtor-non-final-class -Wno-extra-semi-stmt -Wno-misleading-indentation -Wno-quoted-include-in-framework-header -Wno-implicit-fallthrough -Wno-enum-enum-conversion -Wno-enum-float-conversion -Wno-elaborated-enum-base -Wno-reserved-identifier -Wno-gnu-folding-constant -Wno-cast-function-type -Wno-bitwise-instead-of-logical --std=c++17 -fdeprecated-macro -fdebug-compilation-dir=/Users/phil/test_crap -ferror-limit 19 -stack-protector 1 -fstack-check -mdarwin-stkchk-strong-link -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fgnuc-version=4.2.1 -fno-cxx-modules -fcxx-exceptions -fexceptions -fmax-type-align=16 -fcommon -fcolor-diagnostics -clang-vendor-feature=+messageToSelfInClassMethodIdReturnType -clang-vendor-feature=+disableInferNewAvailabilityFromInit -clang-vendor-feature=+disableNonDependentMemberExprInCurrentInstantiation -fno-odr-hash-protocols -clang-vendor-feature=+enableAggressiveVLAFolding -clang-vendor-feature=+revert09abecef7bbf -clang-vendor-feature=+thisNoAlignAttr -clang-vendor-feature=+thisNoNullAttr -mllvm -disable-aligned-alloc-awareness=1 -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /var/folders/n4/lyzh1r8n1wl77pqsjpm6xm440000gn/T/solution-945b01.o -x c++ solution.cpp
clang -cc1 version 14.0.0 (clang-1400.0.29.102) default target arm64-apple-darwin21.6.0
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/local/include"
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/Library/Frameworks"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/14.0.0/include
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks (framework directory)
End of search list.
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -lto_library /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/libLTO.dylib -no_deduplicate -dynamic -arch arm64 -platform_version macos 12.0.0 12.3 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -o a.out -L/usr/local/lib /var/folders/n4/lyzh1r8n1wl77pqsjpm6xm440000gn/T/solution-945b01.o -lc++ -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/14.0.0/lib/darwin/libclang_rt.osx.a

I think the error is maybe actually coming from the linker. g++ --verbose doesn't pass --verbose to the linker, but I know there is some magic invocation that will do that. See if you can find it.

@endecotp I ran the same command and I got this -

% g++ --verbose --std=c++17

Using built-in specs.

COLLECT_GCC=g++

COLLECT_LTO_WRAPPER=/opt/homebrew/Cellar/gcc/12.1.0/bin/../libexec/gcc/aarch64-apple-darwin21/12/lto-wrapper

Target: aarch64-apple-darwin21

Configured with: ../configure --prefix=/opt/homebrew/opt/gcc --libdir=/opt/homebrew/opt/gcc/lib/gcc/current --disable-nls --enable-checking=release --with-gcc-major-version-only --enable-languages=c,c++,objc,obj-c++,fortran --program-suffix=-12 --with-gmp=/opt/homebrew/opt/gmp --with-mpfr=/opt/homebrew/opt/mpfr --with-mpc=/opt/homebrew/opt/libmpc --with-isl=/opt/homebrew/opt/isl --with-zstd=/opt/homebrew/opt/zstd --with-pkgversion='Homebrew GCC 12.1.0' --with-bugurl=https://github.com/Homebrew/homebrew-core/issues --with-system-zlib --build=aarch64-apple-darwin21 --with-sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk

Thread model: posix

Supported LTO compression algorithms: zlib zstd

gcc version 12.1.0 (Homebrew GCC 12.1.0) 

@aarnav, that's the homebrew g++, not the Xcode g++ which is actually clang.

  • but I am getting the same error how do I rectify that? thanks!

Add a Comment

but I am getting the same error how do I rectify that? thanks!

Start by using the Xcode g++ (which is actually clang), not the homebrew g++.

I'm not sure what the best way to do that is; I do have a lot of homebrew packages installed but not any compilers. If you want to have both homebrew and xcode compilers installed it may be sufficient to adjust your PATH - or it may not. I bet this is a Homebrew FAQ.

Or, if you want to get the homebrew compiler working, try asking the homebrew people at e.g. https://github.com/Homebrew/discussions/discussions

Curious to know if @fanick is using the Homebrew g++, deliberately or not?

Add a Comment

@endecotp I am compiling code using gnu compiler instead of clang compiler. On clang it is compiling perfectly but on gnu it is not. By default I am using gnu compiler.

 I am compiling code using gnu compiler instead of clang compiler.

It would have been useful to know that earlier.

I think your best option is to ask the Homebrew people for advice. As noted above, I have a lot of homebrew packages installed but not compilers.

  • Thank you.

Add a Comment