签名只有索引? [英] Signed only index?

查看:48
本文介绍了签名只有索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这个程序打印1为[-1]。我认为它应该是一个

OOB访问情况!


#include< stdio.h>

#include< stdlib.h>


int main()

{

int a [6] = {1,2,3, 4,5};

printf("%d \ n",a [-1]);

返回EXIT_SUCCESS;

}


谢谢,

Vijay Zanvar
http://faq.zanvar.in

Why does this program print 1 for a[-1]. I thought it should be an
OOB access situation!

#include <stdio.h>
#include <stdlib.h>

int main()
{
int a[6]={1,2,3,4,5};
printf("%d\n", a[-1]);
return EXIT_SUCCESS;
}

Thanks,
Vijay Zanvar
http://faq.zanvar.in

推荐答案

" Vijay Kumar R. Zanvar" < vi ***** @ gmail.comwrites:
"Vijay Kumar R. Zanvar" <vi*****@gmail.comwrites:

为什么这个程序打印1为[-1]。我认为应该是一个

OOB访问情况!
Why does this program print 1 for a[-1]. I thought it should be an
OOB access situation!



....

....


int a [6] = {1,2,3,4 ,5};
int a[6]={1,2,3,4,5};



访问越界数组元素的结果是

未定义的行为。这意味着任何事情都可能发生。你的b $ b不能取决于结果,这些结果很可能会从编译器或链接器的一个或多个b $ b运行,甚至程序本身改为

next。

-

"这将是一个更好的未定义行为示例

如果行为未定义。 ;

- 迈克尔鲁宾斯坦

The result of accessing an out-of-bounds array element is
undefined behavior. That means that anything can happen. You
cannot depend on the results, which are likely to change from one
run of the compiler or linker, or even the program itself, to the
next.
--
"It would be a much better example of undefined behavior
if the behavior were undefined."
--Michael Rubenstein


1月11日下午4:52,Vijay Kumar R. Zanvar < vijo ... @ gmail.comwrote:
On Jan 11, 4:52 pm, "Vijay Kumar R. Zanvar" <vijo...@gmail.comwrote:

为什么这个程序打印1为[-1]。我认为它应该是一个

OOB访问情况!

#include< stdio.h>

#include< stdlib.h>


int main()

{

int a [6] = {1,2,3,4,5} ;

printf("%d \ n",a [-1]);

返回EXIT_SUCCESS;

}
Why does this program print 1 for a[-1]. I thought it should be an
OOB access situation!
#include <stdio.h>
#include <stdlib.h>

int main()
{
int a[6]={1,2,3,4,5};
printf("%d\n", a[-1]);
return EXIT_SUCCESS;
}



它打印1(或者更好,它只发生了你打印过的次数

运行它),因为它会调用未定义的行为。

在一个假设数组''foo''中,大小为N,其中N是1到SIZE_MAX,只有

foo [0]到foo [N - 1]是可以访问的。

It prints 1 (or better, it only happened to print 1 the times you have
ran it) because it invokes undefined behavior.
In a hypothetical array ''foo'' of size N where N is 1 to SIZE_MAX, only
foo[0] to foo[N - 1] is accessible.


Vijay Kumar R. Zanvar写道:
Vijay Kumar R. Zanvar wrote:

为什么这个程序打印1为[-1]。我认为它应该是一个

OOB访问情况!


#include< stdio.h>

#include< stdlib.h>


int main()

{

int a [6] = {1,2,3, 4,5};

printf("%d \ n",a [-1]);

返回EXIT_SUCCESS;

}
Why does this program print 1 for a[-1]. I thought it should be an
OOB access situation!

#include <stdio.h>
#include <stdlib.h>

int main()
{
int a[6]={1,2,3,4,5};
printf("%d\n", a[-1]);
return EXIT_SUCCESS;
}



标准''a [-1]''相当于*(a-1),并且就是那个

在数组之外引用你得到未定义的行为...


在我的机器上它只生产了134513293,但是Richard H的系统

可能会产生黄色......

By the standard ''a[-1]'' is equivalent to "*(a-1)", and as that
references outside the array you get Undefined Behaviour...

On my machine it just produced 134513293, but Richard H''s system
may produce a suffusion of yellow...


这篇关于签名只有索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆