抛出例外:阅读访问违规。是nullptr
我试图处理这些要点,但我似乎没有任何努力。
从本质上讲,这将以主运行,并插入数字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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将指针j声明为零指针,
上面的声明等同于
在此中删除无效指针,而循环
调用了不确定的行为。
您得到的是您所做的。
看来您需要将J宣布为至少具有INT类型
和YP的写入
等等。
此外,如果语句(如果将表达式
Primes [*J]
更改为Primes [J]
)是没有意义的。写作会更简单
You declared the pointer j as a null pointer
The above declaration is equivalent to
So dereferencing the null pointer in this while loop
invokes undefined behavior.
What you get is what you do.
It seems you need to declare j as having at least the type int
and yp write
and so on.
Also this if statement (if change the expression
primes[*j]
toprimes [j]
)does not make a great sense. It would be simpler to write