int main()
{
int n = 3;
int a[n] = {1,2,3};
...
in middle of code, value of n can be increased by 10 so I want to have check before cout otherwise a[10] is out of bound.
...
cout<<a[n-1];
return 0;
}
为了解决上述问题,我简单地把一个如果检查。
更正代码:
int main()
{
int n = 3;
int a[n] = {1,2,3};
...
In middle of code, value of n can be increased by 10 so I want to have check before cout otherwise a[10] is out of bound.
...
if(n<= 3)
{
cout<<a[n-1];
}
return 0;
}
是否有其他方法可以避免内存访问越界
我试着用如果检查的基础上的大小。
1条答案
按热度按时间8mmmxcuj1#
可能是
如果n等于或小于-1,或者等于或大于3,则会有多余内存。
在读一[n]之前你可以查一下: