内存管理-C++数组分配分段故障11新手
memory management - C++ Array Allocation Segmentation Fault 11 Newbie
我正在从Robert Sedgewick的《C++中的算法》学习C++。现在我正在研究Eratosthenes筛,它有一个用户指定的最大素数的上界。当我运行最大值为46349的代码时,它会运行并打印出46349之前的所有素数,但当我运行最小值为46350的代码时会出现Segmentation错误。有人能帮忙解释一下原因吗?
./sieve.exe 46349
2 3 5 7 11 13 17 19 23 29 31 ...
./sieve.exe 46350
Segmentation fault: 11
代码:
#include<iostream>
using namespace std;
static const int N = 1000;
int main(int argc, char *argv[]) {
int i, M;
//parse argument as integer
if( argv[1] ) {
M = atoi(argv[1]);
}
if( not M ) {
M = N;
}
//allocate memory to the array
int *a = new int[M];
//are we out of memory?
if( a == 0 ) {
cout << "Out of memory" << endl;
return 0;
}
// set every number to be prime
for( i = 2; i < M; i++) {
a[i] = 1;
}
for( i = 2; i < M; i++ ) {
//if i is prime
if( a[i] ) {
//mark its multiples as non-prime
for( int j = i; j * i < M; j++ ) {
a[i * j] = 0;
}
}
}
for( i = 2; i < M; i++ ) {
if( a[i] ) {
cout << " " << i;
}
}
cout << endl;
return 0;
}
此处存在整数溢出:
for( int j = i; j * i < M; j++ ) {
a[i * j] = 0;
}
CCD_ 1不适合CCD_。
在我的机器上,将j
的类型更改为long
可以为更大的输入运行程序:
for( long j = i; j * i < M; j++ ) {
根据编译器和体系结构的不同,您可能必须使用long long
才能获得相同的效果。
当您使用调试器运行程序时,您会看到它在上失败
a[i * j] = 0;
CCD_ 6溢出并变为负。这个负数小于M
,这就是为什么它再次进入循环,然后在访问a[-2146737495]
时失败。
我明白了,问题是将M声明为int。当我将I、M和j声明为长时,这似乎很好。
在任何合理现代的C++中,如果分配失败,除非使用非抛出的new,否则不会从new返回null指针。代码的这一部分不会像您期望的那样工作——您必须捕获可能从对46349 * 46349
0的调用中发出的std::bad_alloc
。
您还需要将数组索引声明为类型size_t
。
相关文章:
- 分段故障(堆芯转储)矢量
- C++中的动态铸造故障
- 数组的指针从不分段故障
- vscode g++链路故障:体系结构x86_64的未定义符号
- 访问被拒绝后,c++中的故障保护代码
- Windows 10-使用gtkmm-3.0库和g++[包括再现]的分段故障
- 我是C++编程的新手,这些代码之间有什么区别,我应该使用哪一个
- 我是c ++的新手,你能解释一下在这种情况下的指针吗
- 调试 CUDA MMU 故障
- 我是 c++ 的新手.学习基本知识后,我想做井字游戏.对于印刷板,我在下面写代码,但它显示错误
- 我是编程新手
- Geeksforgeeks C 程序故障排除:IEE 754 表示法为十进制
- C++新手,想知道如何使用VS code 2019播放音频文件
- Arch Linux.AUR 包 mysql 不能用 makepkg 构建.错误:构建 () 中出现故障
- 正在处理故障(堆芯转储)
- 在 Boost::fiber 中引发的BOOST_ASSERT故障 Visual Studio "Debug" 构建
- 如何进行故障排除:未定义对"非虚拟 thunk to ..."的引用
- C++功能泄漏内存,我是C++新手,不确定如何解决
- C++函数过载会导致 SEG 故障
- 内存管理-C++数组分配分段故障11新手