简体   繁体   中英

C++:Linker command with exit code -1 in xcode

I was implementing a suffix array in xcode using c++ when I got the following error:

ld: 32-bit RIP relative reference out of range (100000121018926 max is +/-4GB): from _main (0x100001310) to _L (0x5AF417B0F130) in '_main' from /Users/priya/Library/Developer/Xcode/DerivedData/cfquestions-boqlvazrozappdeetfhesfsohczs/Build/Intermediates/cfquestions.build/Debug/cfquestions.build/Objects-normal/x86_64/main.o for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

My code for the suffix array is as follows:

char a[size1];
int ans[size2][size2];
struct node{
    int gg[2],pos;
}L[size1];
int step=1,ct=1;
bool comp(node a, node b){
    return a.gg[0]==b.gg[0]?(a.gg[1]<b.gg[1]?1:0):(a.gg[0]<b.gg[0]?1:0);
}
int main(){
    int TT;
    cin>>TT;
    while(TT--){
        set<int> s;
        //a.clear();
        int n=strlen(a);
        scanf("%s",a);
        for(int i=0;i<strlen(a);i++)ans[0][i]=a[i]-'a';
        for(;ct<strlen(a);step++,ct<<=1){
            for(int i=0;i<n;i++){
                L[i].gg[0]=ans[step-1][i];
                L[i].gg[1]=i+ct<n?ans[step-1][i+ct]:-1;
                L[i].pos=i;
            }
            sort(L,L+n,comp);
            for(int i=0;i<n;i++)
                ans[step][L[i].pos]=i>0&&L[i].gg[0]==L[i-1].gg[0]&&L[i].gg[1]==L[i-1].gg[1]?ans[step][L[i-1].pos]:i;
        }
        for(int i=0;i<n;i++)
        {
            if(s.find(ans[step-1][i])!=s.end()){

            }
            else s.insert(ans[step-1][i]);
        }
        cout<<s.size()<<endl;
    }
    return 0;
}

PS: The code runs fine on any other code. I tried even much more complex codes; but it works fine. Hence there must be something wrong with this piece of code- but I am not able to figure out what!

Any help would be appreciated thanks!!

edit: The reason is the size of ans , as pointed out in the comment by @WhozCraig after you added the sizes. My guess regarding why the linker error message names _L is that the compiler put main at a lower address and then the global data in order, giving the too big RIP offset to L .


In one of the for loops you have i = 0 and access L[i-1] , which is a very large number for the array index .

edit: but that should give a runtime failure or error and not give a linker error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM