运行时错误,使我的 .exe 崩溃,我不知道为什么

发布于 2024-12-26 11:28:21 字数 1709 浏览 2 评论 0原文

我可以猜测这与使用 unsigned long long int 有关。

#include <cstdlib>
#include <iostream>
#include <cmath>

using namespace std;

typedef unsigned long long int uint64;

int main(int argc, char *argv[])
{



    uint64 number_in_question = 600851475143LL;

    long double sqrt_in_question = sqrt(number_in_question);
    bool primes_array[number_in_question+1];



    for (uint64 i = 0; i <= number_in_question; i++) {
        primes_array[i] = true;
    }

    for (uint64 i = 2; i <= sqrt_in_question; i++) {
        if(primes_array[i] == true) {
            // for every multiple of this prime, mark it as not prime
            for (uint64 ii = i*2; ii <= number_in_question; ii += i) {
                primes_array[ii] = false;           
            }
        }
    }   

    for (uint64 i = 0; i <= number_in_question; i++) {
        if(primes_array[i] == true)
        cout << i << ", ";
    }


    system("PAUSE");


    return EXIT_SUCCESS;
}

编辑1: 我想做的一些背景:

我试图模仿这种技术: http://en.wikipedia .org/wiki/Sieve_of_Eratosthenes 当我使用数组来存储一个简单的“它是素数”时,1 表示是,0 表示否。最终目标是解决这个问题:

数字 600851475143 的最大质因数是多少?此处列出:http://projecteuler.net/problem=3。我只是研究素数,然后再研究素数因子。

Edit2:

查看我发布的维基百科链接后,我意识到他们有 puesdocode (跳过它并提出了我所拥有的)并意识到有以下注释: 大范围可能无法完全装入内存。在这些情况下,有必要使用分段筛,一次仅筛分范围的一部分。 [14]对于范围太大以致筛分素数无法保存在内存中的情况,可以使用像 Sorenson 那样节省空间的筛子。 因此,我必须想出一种使用“分段筛”方法来做到这一点的方法。

Edit3:

更改了数组以考虑 [0] 元素,因此“问题”仅集中在数组内存大小对于将来的引用来说太大;还将数组存储为 bool 而不是 uint64。

I can take a guess that it has something to do with working with the unsigned long long int.

#include <cstdlib>
#include <iostream>
#include <cmath>

using namespace std;

typedef unsigned long long int uint64;

int main(int argc, char *argv[])
{



    uint64 number_in_question = 600851475143LL;

    long double sqrt_in_question = sqrt(number_in_question);
    bool primes_array[number_in_question+1];



    for (uint64 i = 0; i <= number_in_question; i++) {
        primes_array[i] = true;
    }

    for (uint64 i = 2; i <= sqrt_in_question; i++) {
        if(primes_array[i] == true) {
            // for every multiple of this prime, mark it as not prime
            for (uint64 ii = i*2; ii <= number_in_question; ii += i) {
                primes_array[ii] = false;           
            }
        }
    }   

    for (uint64 i = 0; i <= number_in_question; i++) {
        if(primes_array[i] == true)
        cout << i << ", ";
    }


    system("PAUSE");


    return EXIT_SUCCESS;
}

Edit1:
Some background of what I am trying to do:

I am trying to mimic this technique: http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
while I am using an array to store a simple "is it prime" 1 for yes, 0 for no. The end goal is to solve this:

What is the largest prime factor of the number 600851475143 ? Listed here: http://projecteuler.net/problem=3. I am just working on the primes and then will work on the prime factors.

Edit2:

Upon looking at the Wikipedia link I posted, I realized they have puesdocode (skipped over that and came up with what I have) and realized that had this note:
Large ranges may not fit entirely in memory. In these cases it is necessary to use a segmented sieve where only portions of the range are sieved at a time.[14] For ranges so large that the sieving primes could not be held in memory, space-efficient sieves like that of Sorenson are used instead.
Therefore I will have to think of a way to do this using a "segmented sieve" method.

Edit3:

Changed the array to account for the [0] element so the "issue" is only focused on the array memory size being too large for future references; also stored the array as a bool instead of a uint64.

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

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

发布评论

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

评论(3

千纸鹤 2025-01-02 11:28:21

您正在尝试分配长度为 600851475143uint64 数组。对于 8 字节 uint64 来说,这意味着该数组将占用 600851475143*8byte,大约为 4.5TB 内存。即使您的系统可以分配那么多内存(不太可能),您也会尝试将其放在堆栈上,而堆栈的大小通常仅限于几 MB。此外,您尝试写入索引 number_in_question,而数组中的最后一个索引是 number_in_question-1

You are trying to allocate an uint64 array of length 600851475143. For 8 byte uint64 that means this array will take up 600851475143*8byte which is roughly 4.5TB of memory. Even if your system can allocate that much memory (unlikely) you are trying trying to put it on the stack which has typically a size bound to only a few MB. Furthermore you are trying to write to index number_in_question, while the last index in the array is number_in_question-1.

未央 2025-01-02 11:28:21

我假设你在尝试创建数组时正在破坏堆栈。该数组的大小非常大,必须在堆上创建才能有成功的机会。

I would assume your are blowing the stack when it attempts to create the array. That size of array is tremendously large and would have to be created on the heap to even have a chance of succeeding.

吻风 2025-01-02 11:28:21

你的数组有“number_in_question”元素,编号为 0 到 number_in_question - 1。但是,如果将这么大的数组分配给你,你的 for 循环将尝试访问 primes_array[number_in_question],它超出了可用空间。

为什么要分配一个 uint64 数组并在每个元素中存储 0 或 1?

编辑:另外,您不能在堆栈上分配具有这样的可变大小的数组。您必须使用 new 来在堆上分配该空间。再次假设您的系统允许您分配那么多空间。

Your array has "number_in_question" elements, numbered 0 to number_in_question - 1. But, your for loop will try to access primes_array[number_in_question], which is outside the available space, if that large of an array will even be allocated to you.

Why are you allocating an array of uint64 and storing either 0 or 1 in each element?

Edit: also, you can't allocate an array on the stack with a variable size like that. You'd have to use new to allocate that space on the heap. Again, assuming your system will allow you to allocate that much space.

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