簡體   English   中英

如何修復 C 中的“程序因信號 SIGBUS,總線錯誤而終止”

[英]How to fix 'Program terminated with signal SIGBUS, Bus error' in C

我在名為“均衡陣列”的 HackerRank 上遇到了這個問題, https: //www.hackerrank.com/challenges/equality-in-a-array/problem

我已經完成了這個程序,它在我的編譯器上正常工作,但是當我在 HackerRank 網站上使用相同的代碼時,它不起作用。 它向我展示了這個錯誤

GDB trace:
Reading symbols from solution...done.
[New LWP 4647]
Core was generated by `solution'.
Program terminated with signal SIGBUS, Bus error.
#0  0x00005581998066b0 in main () at solution.c:15
15          scanf("%ld",&arr[i]);
#0  0x00005581998066b0 in main () at solution.c:15

我的代碼是:-

#include<stdio.h>
int main()
{
    long int arr[100];
    long int n,i,j;
    long int larg=0, largno=0;
    scanf("%d",&n);
    long int iarr[n];
    long int max=0;
    long int out=0,fout=0;

for(i=0;i<n;i++)
{
    scanf("%ld",&arr[i]);
}


for(j=0;j<n;j++)
{
    max=0;
    for(i=0;i<n;i++)
    {
        if(arr[j]==arr[i])
        {
            max++;
        }
    }
    iarr[j]=max;
}


for(j=0;j<n;j++)
{
    if(larg < iarr[j])
    {
     larg = iarr[j];
     largno = j;
    }
}

for(i=0;i<n;i++)
{
    if(arr[largno]==arr[i])
    {
        out++;
    }

}
fout=n-out;
printf("%ld",fout);

}

請幫我解決這個問題:)

您需要驗證n是否在 [1, 99] 范圍內。

scanf("%d", &n)需要一個int並且您傳遞一個long

驗證您的格式字符串,檢查返回值是否有錯誤,並確保這些值在可接受的范圍內。

始終使用-Wall -Wextra編譯您的代碼,這些也會報告格式字符串和參數類型不匹配。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM