抛出例外:阅读访问违规。是nullptr

发布于 2025-02-10 01:57:32 字数 1078 浏览 3 评论 0原文

我试图处理这些要点,但我似乎没有任何努力。

从本质上讲,这将以主运行,并插入数字n,以找到从0到n的所有质量数。然后将其写入CSV文件中。

IDE还说J是无效的指针。

#include <iostream>
#include "Prime.h"
#include <fstream>


using namespace std;

bool IsInteger(double d) {

    return(d == (int)d);
}

int ComputePrimes(int primes[], int max) {
    int numprimes = 1;
    primes[0] = 2;

    

    for (int i = 3; i <= max; i++) {

        int* j = 0;
        
        bool prime = true;

        while ((prime == true) && (primes[*j] <= (i / 2))) { ///The j is has problems
            
        if (IsInteger((double)i / (double)primes[*j])) {
            
            prime = false;
        
                
            }
        j++;
        }
        if (prime) {
            primes[numprimes++] = i;

        }
    }
    return numprimes;
}

void PrintPrimes(int numprimes, int primes[], string filename) {

    fstream outfile;

    outfile.open(filename);

    for (int i = 0; i < numprimes; i++) {

        outfile << primes[i] << endl;

    }

}

需要更改什么?

I tried to work with the points but nothing I do seem to work.

Essentially this will be run in main and a number n is inserted to find all the prime numbers from 0 to n. This is then written into a csv file.

The IDE also says that j is a null pointer.

#include <iostream>
#include "Prime.h"
#include <fstream>


using namespace std;

bool IsInteger(double d) {

    return(d == (int)d);
}

int ComputePrimes(int primes[], int max) {
    int numprimes = 1;
    primes[0] = 2;

    

    for (int i = 3; i <= max; i++) {

        int* j = 0;
        
        bool prime = true;

        while ((prime == true) && (primes[*j] <= (i / 2))) { ///The j is has problems
            
        if (IsInteger((double)i / (double)primes[*j])) {
            
            prime = false;
        
                
            }
        j++;
        }
        if (prime) {
            primes[numprimes++] = i;

        }
    }
    return numprimes;
}

void PrintPrimes(int numprimes, int primes[], string filename) {

    fstream outfile;

    outfile.open(filename);

    for (int i = 0; i < numprimes; i++) {

        outfile << primes[i] << endl;

    }

}

What needs to be changed?

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

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

发布评论

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

评论(1

夢归不見 2025-02-17 01:57:32

您将指针j声明为零指针,

int* j = 0;

上面的声明等同于

int* j = nullptr;

在此中删除无效指针,而循环

while ((prime == true) && (primes[*j] <= (i / 2))) {
                                 ^^^^

调用了不确定的行为。

您得到的是您所做的。

看来您需要将J宣布为至少具有INT类型

int j = 0;

和YP的写入

while ((prime == true) && (primes[j] <= (i / 2))) {

等等。

此外,如果语句(如果将表达式Primes [*J]更改为Primes [J]

    if (IsInteger((double)i / (double)primes[j])) {
        
        prime = false;
    
            
        }

是没有意义的。写作会更简单

    if ( i % primes[j] == 0 ) {
        
        prime = false;
    
            
        }

You declared the pointer j as a null pointer

int* j = 0;

The above declaration is equivalent to

int* j = nullptr;

So dereferencing the null pointer in this while loop

while ((prime == true) && (primes[*j] <= (i / 2))) {
                                 ^^^^

invokes undefined behavior.

What you get is what you do.

It seems you need to declare j as having at least the type int

int j = 0;

and yp write

while ((prime == true) && (primes[j] <= (i / 2))) {

and so on.

Also this if statement (if change the expression primes[*j] to primes [j])

    if (IsInteger((double)i / (double)primes[j])) {
        
        prime = false;
    
            
        }

does not make a great sense. It would be simpler to write

    if ( i % primes[j] == 0 ) {
        
        prime = false;
    
            
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文