我的代码哪里存在访问冲突?

发布于 2024-12-07 16:17:42 字数 1817 浏览 2 评论 0原文

当我尝试运行此代码来查找边界内的素数时,出现访问冲突。

int main () {
    cout << "Program initialized successfully. Please wait for the next message to appear." << endl << endl ;

    int Primes[51] ;
    int runner = 0 ;
    int chaser = 0 ;
    int bound = 0 ;
    int count = 0 ;

    cout << "Please enter the maximum boundary of the calculation : " ;

    cin >> bound ;

    cout << endl << "The number you've entered, " << bound << ", has been accepted. Please wait for the calculations." << endl ;

    if (runner <= bound ) {
        Primes[0] = 2;
        Primes[1] = 3;
        Primes[2] = 5;
        Primes[3] = 7;
        Primes[4] = 11;
        count = 4;

        for ( runner = 11 ; runner <= bound ; runner ++ ) {
            while ( runner % Primes[chaser] != 0 ) {
                for ( chaser = 0 ; Primes[chaser] != 0 ; chaser ++ ) {
                    if ( runner % Primes[chaser] == 0 ) {
                        count ++ ;
                        Primes[count] = runner;
                    }
                }
            }
        }

        int chaser_count;
        cout << "Here's the primes computer discovered : " << endl ;
        for ( chaser_count = 0 ; chaser_count <= count ; chaser_count ++ ) {
            cout << Primes[chaser_count] << endl ;
        }
        cout << "There is " << count << " primes discovered." << endl ;
    }
    return 0;
}

程序运行良好,直到计算行: if(runner <=bound)

我遇到访问冲突。

我有点知道什么是访问冲突,但我不知道是什么引发了它。

编辑:

我现在得到 2 个答案,指出我可能有类似 Primes[50] 的情况,但我严重怀疑这一点,因为在指定边界后我立即收到错误,12。

感谢取消评论的人。

我正在使用 Dev-C++。

我找到了引发错误的地方。感谢任何为我发表评论和回答的人。我没有发现这是一个导致 Prime 的逻辑错误[51]。

谢谢大家对我的帮助。

I'm getting an Access Violation when attempting to run this code which finds prime numbers in a bound.

int main () {
    cout << "Program initialized successfully. Please wait for the next message to appear." << endl << endl ;

    int Primes[51] ;
    int runner = 0 ;
    int chaser = 0 ;
    int bound = 0 ;
    int count = 0 ;

    cout << "Please enter the maximum boundary of the calculation : " ;

    cin >> bound ;

    cout << endl << "The number you've entered, " << bound << ", has been accepted. Please wait for the calculations." << endl ;

    if (runner <= bound ) {
        Primes[0] = 2;
        Primes[1] = 3;
        Primes[2] = 5;
        Primes[3] = 7;
        Primes[4] = 11;
        count = 4;

        for ( runner = 11 ; runner <= bound ; runner ++ ) {
            while ( runner % Primes[chaser] != 0 ) {
                for ( chaser = 0 ; Primes[chaser] != 0 ; chaser ++ ) {
                    if ( runner % Primes[chaser] == 0 ) {
                        count ++ ;
                        Primes[count] = runner;
                    }
                }
            }
        }

        int chaser_count;
        cout << "Here's the primes computer discovered : " << endl ;
        for ( chaser_count = 0 ; chaser_count <= count ; chaser_count ++ ) {
            cout << Primes[chaser_count] << endl ;
        }
        cout << "There is " << count << " primes discovered." << endl ;
    }
    return 0;
}

The program runs fine until to the line of calculation : if(runner <= bound)

I got a Access Violation.

I Kind of know what an Access Violation is, but I don't know what raised it.

edit:

I got 2 answers now stating that I may have something like Primes[50] going on, but I seriously doubt so, because I get the error immediately after I specify the bound, 12.

Thanks for the guy who de-comment this.

I'm using Dev-C++.

I found the place where the error was raised. Thanks for anyone who commented and answered for me. It is an logical error that I didn't found that leads to a Prime[51].

Thank you all for helping me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

任谁 2024-12-14 16:17:42

这里:

for ( chaser = 0 ; Primes[chaser] != 0 ; chaser ++ ) {

你没有用 0 初始化你的 Primes 数组,所以循环可以一遍又一遍地循环,chaser 可能大于 51(Primes 数组的大小),然后 Primes[something_bigger_than_50] 将引发访问冲突。

Here :

for ( chaser = 0 ; Primes[chaser] != 0 ; chaser ++ ) {

you didn't initialize you Primes array with 0, so the loop can loop over and over and chaser can be bigger than 51 (the size of you Primes array) and then Primes[something_bigger_than_50] will raise an access violation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文