该代码在调试模式下工作,但在发布模式C 中不起作用
This code works in debug mode but not in release mode c++
我正在尝试生成高达10亿的质数。此代码在调试模式下运行良好,但是在发行模式下它会在2后崩溃3. 3.有人可以在这里提供帮助吗?
void SieveOfEratosthenes(size_t n)
{
// Create a boolean array "prime[0..n]" and initialize
// all entries it as true. A value in prime[i] will
// finally be false if i is Not a prime, else true.
bool* prime = new bool[1000000000];
memset(prime, true, sizeof(prime));
for (size_t p = 2; p*p <= n; p++)
{
// If prime[p] is not changed, then it is a prime
if (prime[p] == true)
{
// Update all multiples of p
for (size_t i = p * 2; i <= n; i += p)
prime[i] = false;
}
}
// Prsize_t all prime numbers
for (size_t p = 2; p <= n; p++)
if (prime[p])
cout << p << " ";
}
int main()
{
size_t n = 1000000000;
cout << "Following are the prime numbers smaller "
<< " than or equal to " << n << endl;
SieveOfEratosthenes(n);
getchar();
return 0;
}
一个问题是memset
调用是错误的。您需要称其为memset(prime, true, sizeof(bool) * 1000000)
。sizeof(prime)
将返回8
,这是您正在测试的计算机上最有可能的指针的大小。
这里有两个主要问题。
首先,这是不正确的:
memset(prime, true, sizeof(prime));
表达式sizeof(prime)
为您提供了指针的大小(最有可能是4或8),而不是指向的。因此,您只设置4或8个字节。您可以这样做:
memset(prime, true, sizeof(*prime) * n`);
然而,这取决于sizeof(*prime)
为1。如果不是,则这些值不会是您的期望。正确的方法是:
for (i=0; i<n; i++) {
prime[i] = true;
}
另一个问题是您的for
循环中的限制:
for (size_t p = 2; p*p <= n; p++)
...
for (size_t i = p * 2; i <= n; i += p)
...
for (size_t p = 2; p <= n; p++)
在每个中,您都在检查您用作数组索引的循环计数器是否为 <= n
。由于您的数组具有n
元素,因此此数组中的有效索引从0到n-1
。而且,由于当索引为n
时,您不会退出循环,因此您最终会在数组的末端读取/写作。这调用了未定义的行为。
您需要将这些循环条件更改为< n
:
for (size_t p = 2; p*p < n; p++)
...
for (size_t i = p * 2; i < n; i += p)
...
for (size_t p = 2; p < n; p++)
另外,在SieveOfEratosthenes
的末尾,请务必 delete[] prime;
,以免泄漏内存。
相关文章:
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 为什么在保护模式下继承升级不起作用
- 如何在全屏模式下(在OpenGL中)使背景透明
- 为什么使用__LINE_的代码在发布模式下在MSVC下编译,而不是在调试模式下
- 派生类是否可以在抽象工厂设计模式中具有数据成员
- 此模式的C++RegEx
- avrogencpp能为模式中的每种类型生成单独的头文件吗
- 使用可变模板的Broadcaster/Listener模式
- c++方法参数只能在linux的发布模式下自行更改
- 资源管理设计模式
- 使用 mod_gsoap 部署服务时,如何在 Gsoap 中更改 soap 上下文的模式?
- C++ 无法在字符数组中使用 for 循环打印字母模式
- 小字符串优化(调试与发布模式)
- 多对多请求/结果模式的模式
- 正则表达式用模式替换模式
- QRegExp :搜索不包含其他模式的模式
- 设计模式(GoF模式)在c++中的实现
- 如何对某个十进制模式进行模式匹配
- 将 valijson 与 Nlohmann 的 JSON for Modern C++ 结合使用,以验证具有子模式的模式
- 消息系统的观察者模式+访问者模式