[在此发现一个疑问:C++ - 检测超出范围的访问 ]
如果我有一个"超出范围矢量访问"的程序,像这样:
std::vector<int> A(2);
...
A[10] = 3;
Run Code Online (Sandbox Code Playgroud)
我有办法确定找到这个错误吗? 我的意思是在调试模式下编译并查看某些断言是否会停止执行.
到目前为止,我已经自己查了一下.但可能是我不必编写额外的代码?
PS我当然检查过断言.它没有打电话.
有了这个程序:
#include <vector>
int main() {
std::vector<int> A(2);
A[10] = 3;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
汇编
g++ 1.cpp -O0; ./a.out
Run Code Online (Sandbox Code Playgroud)
所以看起来std在代码中没有断言,我不禁想知道为什么他们不做这么简单的检查.
在C中,代码片段:
int i;
double x[10];
for (i = 0; i <= 10; i++) {
x[i] = (double) i;
printf("%f\n", x[i]);
}
Run Code Online (Sandbox Code Playgroud)
产生以下输出:
0.000000
1.000000
2.000000
3.000000
4.000000
5.000000
6.000000
7.000000
8.000000
9.000000
10.000000
Run Code Online (Sandbox Code Playgroud)
但是,在Java中,代码片段(相同的代码)只是语法上的不同:
int i;
double[] x = new double[10];
for (i = 0; i <= 10; i++) {
x[i] = (double) i;
System.out.printf("%f\n", x[i]);
}
Run Code Online (Sandbox Code Playgroud)
产生以下输出:
0.000000
1.000000
2.000000
3.000000
4.000000
5.000000
6.000000
7.000000
8.000000
9.000000
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at Array.main(Array.java:14)
Run Code Online (Sandbox Code Playgroud)
为什么C编译器没有标记在数组边界外访问的尝试?我在这里错过了什么吗?
如果下标超过C中数组的大小,为数组赋值时会发生什么?
在其他语言中,它会引发异常,C是否也会抛出异常?出于某种原因,我没有得到任何错误
我有一个小代码来查看它argv的内容
#include <iostream>
using namespace std;
int main(int argc, char** argv){
cout << "===================" << endl;
cout << "argc: " << argc << endl << endl;
cout << "argv[0]: " << argv[0] << endl << endl;
cout << "argv[1]: " << argv[1] << endl << endl;
//cout << "argv[2]: " << argv[2] << endl << endl; //Program crash
cout << "===================" << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我启动没有参数的程序时:
argc1
argv[0]是执行路径
argv[1]应该超出范围,程序应该崩溃,但程序终止像我写return.如果我检查错误级别它是9009
如果我尝试访问argv[2]程序崩溃按预期 …
我在玩不安全的Rust时遇到了这种奇怪的现象.我认为这段代码应该会出现分段错误,但事实并非如此.我错过了什么吗?我试图设置一个指向一个生命周期较短的变量的指针,然后取消引用它.
// function that sets a pointer to a variable with a shorter lifetime
unsafe fn what(p: &mut *const i32) {
let a = 2;
*p = &a;
//let addr = *p; // I will talk about this later
println!("inside: {}", **p);
}
fn main() {
let mut p: *const i32 = 0 as *const i32;
unsafe {
what(&mut p);
// I thought this line would make a segfault because 'a' goes out of scope at the end of the …Run Code Online (Sandbox Code Playgroud) 如果a声明一个大小为4的数组并初始化它。
int arr[4]={1,2,3,4} 为什么我执行 arr[4] =5 或任何其他索引时没有错误。而数组的大小是 4 。为什么没有错误。这背后的逻辑是什么?
我知道没有错误。但我无法理解这背后的逻辑。
这是我的最小可重现示例:
#include <stdio.h>
int main( int argc, char* argv[])
{
printf (" this is the contents of argc:%d\n",argc);
int i;
for (i = 0; i < argc ; i++){
printf(" argv = %d = %s\n",i,argv[i]);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我argc将 for 循环更改为数字时,比方说10,代码在到达之前崩溃10:
$ ./argc one two three
this is the contents of argc:4
argv = 0 = ./argc
argv = 1 = one
argv = 2 = two
argv = 3 = three
argv …Run Code Online (Sandbox Code Playgroud) #include <iostream>
using namespace std;
int a[4];
int main(){
int b=7;
cout<<a[b]<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我试图访问未分配的内存.所以我希望输出是一个分段错误,而输出是一些垃圾值.
后来我将'b'增加到1000,现在输出是'Segmentation fault'.这种行为有特定的原因吗?
我正在使用gcc-4.3.2编译器.
为什么这没有任何错误?
int array[2];
array[5] = 21;
cout << array[5];
Run Code Online (Sandbox Code Playgroud)
打印出21就好了.但看看这个!我改变了5到46,它仍然有效.但是当我放47时,它没有打印任何东西.在任何地方都没有显示任 那是怎么回事!?!?
我有一些在R函数中运行的cpp代码,调用大约80k次.它的测试套件是全面的并且通过.它的前60k次似乎运行良好,然后在中间的某个地方,我得到了一个段错误.
*** Error in `/usr/lib/R/bin/exec/R': malloc(): memory corruption: 0x00000000047150f0 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x77725)[0x7f684462e725]
/lib/x86_64-linux-gnu/libc.so.6(+0x819be)[0x7f68446389be]
/lib/x86_64-linux-gnu/libc.so.6(__libc_malloc+0x54)[0x7f684463a5a4]
/usr/lib/R/lib/libR.so(Rf_allocVector3+0x70d)[0x7f6844cd617d]
... # more
Run Code Online (Sandbox Code Playgroud)
以下是我的一些代码示例,你能看到它有什么问题吗?
返回一个LogicalVector(例如TRUE/ FALSEvector),其中前导NAs被标记为TRUE
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
LogicalVector leading_na(IntegerVector x) {
int n = x.size();
LogicalVector leading_na(n);
int i = 0;
while(x[i] == NA_INTEGER) {
leading_na[i] = TRUE;
i++;
}
return leading_na;
}
Run Code Online (Sandbox Code Playgroud)
返回一个LogicalVector(例如TRUE/ FALSEvector),其中尾随NAs被标记为TRUE
// [[Rcpp::export]]
LogicalVector trailing_na(IntegerVector x) { …Run Code Online (Sandbox Code Playgroud)