指向数组的指针 - 是否有超出限制的访问权限? [英] pointer to an array -Is there an out of bound access?

查看:48
本文介绍了指向数组的指针 - 是否有超出限制的访问权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int main(){


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

int(* ptr)[3] = a;


/ *这应该没问题,并输出3作为输出* /

printf( %d \ n,(* ptr)[2]);


++ ptr;


/ *这应该没问题,给6作为输出* /

printf("%d \ n",(* ptr)[2]);


返回0;

}


但如果我这样做会怎么样


int main(){


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

int(* ptr)[ 3] = a;


/ *这应该没问题,并输出3作为输出* /

printf("%d \ n, * ptr)[2]);


/ *这会没关系吗?

printf("%d \ n",(* ptr )[5]);

返回0;

}


第二个代码中的第二个printf调用是否正常?或是它是否出界

访问?

我用gcc 3.4.5和-Wall和-pedantic尝试了两个代码。

没有警告,并在两个代码中得到正确的输出为3和6.

int main (){

int a[][3]={{1,2,3},{4,5,6}};
int (*ptr)[3]=a;

/* This should be fine and give 3 as output*/
printf("%d\n",(*ptr)[2]);

++ptr;

/* This should be fine and give 6 as output*/
printf("%d\n",(*ptr)[2]);

return 0;
}

But what if I do something like this

int main (){

int a[][3]={{1,2,3},{4,5,6}};
int (*ptr)[3]=a;

/* This should be fine and give 3 as output*/
printf("%d\n",(*ptr)[2]);

/* Will this be fine??
printf("%d\n",(*ptr)[5]);
return 0;
}

Is the second printf call in second code fine? or is it out of bound
access?
I tried both the codes with gcc 3.4.5 with -Wall and -pedantic. There
was no warning and got the correct output as 3 and 6 in both codes.

推荐答案




10月28日上午11:43,Kavya < Lerne ... @ gmail.comwrote:


On Oct 28, 11:43 am, "Kavya" <Lerne...@gmail.comwrote:

int main(){


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

int(* ptr)[3] = a;


/ *这应该没问题,并输出3作为输出* /

printf("%d \ n",(* ptr)[2]);


++ ptr;


/ *这应该没关系,输出6作为输出* /

printf("%d \ n" ;,(* ptr)[2]);


返回0;


}但是,如果我做这样的事情怎么办? >

int main(){


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

int(* ptr)[3] = a;


/ *这应该没问题,并输出3作为输出* /

printf("%d \ n",(* ptr)[2]);


/ *这样会好吗??

printf("%d \ n",(* ptr)[5]);

返回0;


}是第二个printf打电话给第二个代码罚款?或是它是否出界

访问?

我用gcc 3.4.5和-Wall和-pedantic尝试了两个代码。

没有警告,并在两个代码中得到正确的输出为3和6。
int main (){

int a[][3]={{1,2,3},{4,5,6}};
int (*ptr)[3]=a;

/* This should be fine and give 3 as output*/
printf("%d\n",(*ptr)[2]);

++ptr;

/* This should be fine and give 6 as output*/
printf("%d\n",(*ptr)[2]);

return 0;

}But what if I do something like this

int main (){

int a[][3]={{1,2,3},{4,5,6}};
int (*ptr)[3]=a;

/* This should be fine and give 3 as output*/
printf("%d\n",(*ptr)[2]);

/* Will this be fine??
printf("%d\n",(*ptr)[5]);
return 0;

}Is the second printf call in second code fine? or is it out of bound
access?
I tried both the codes with gcc 3.4.5 with -Wall and -pedantic. There
was no warning and got the correct output as 3 and 6 in both codes.



它已经结束了。

我也使用gcc并得到以下内容:


3

-1074239864

..事实上,''ptr''不是常见的int指针。请参阅以下内容:


int * p =(int *)a;

printf("%d \ nn",p [5]) ;


那没关系,因为''p''是一个常见的int指针。

It''s out.
I also use gcc and got the following:

3
-1074239864

.. In fact, ''ptr'' is NOT a common int pointer. See the following:

int *p = (int*)a;
printf("%d\n", p[5]);

That''s OK, because ''p'' is a common int pointer.


Kavya:
Kavya:

int main(){


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

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



相当于:


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


Equivalent to:

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


int(* ptr)[3] = a;
int (*ptr)[3]=a;



相当于:


int(* ptr)[3] =& a [0];


Equivalent to:

int (*ptr)[3] = &a[0];


/ *这应该没问题,并输出3作为输出* /

printf("%d \ n", * PTR)[2]);
/* This should be fine and give 3 as output*/
printf("%d\n",(*ptr)[2]);



相当于:


printf("%d \ n",a [0] [2]) ;


Equivalent to:

printf("%d\n", a[0][2]);


++ ptr;
++ptr;



相当于:


ptr =& a [1];


Equivalent to:

ptr = &a[1];


/ *这应该没问题,并输出6作为输出* /

printf("%d \ n",(* ptr)[2]) ;
/* This should be fine and give 6 as output*/
printf("%d\n",(*ptr)[2]);



相当于:


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


Equivalent to:

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


返回0;

}
return 0;
}



那个工作正常。


That works fine.


但是如果我这样做会怎么样


int main(){


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

int(* ptr)[ 3] = a;


/ *这应该没问题,并输出3作为输出* /

printf("%d \ n, * ptr)[2]);


/ *这会没关系吗?

printf("%d \ n",(* ptr )[5]);
But what if I do something like this

int main (){

int a[][3]={{1,2,3},{4,5,6}};
int (*ptr)[3]=a;

/* This should be fine and give 3 as output*/
printf("%d\n",(*ptr)[2]);

/* Will this be fine??
printf("%d\n",(*ptr)[5]);



基本上你要问的是标准是否需要多维

数组以特定的方式存在于内存中,特别是:


a [0] [0] a [0] [1] a [0] [2] a [1] [0] a [1] [1] a [1 ] [2]


我很确定事情必须是这样的,但也许有人可以

给出标准证明或反对必要性。


-


Frederick Gotham


Basically you''re asking if the Standard necessitates that multi-dimensional
arrays be lain out in a particular way in memory, specifically:

a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2]

I''m pretty sure that things have to be like this, but maybe someone can
give a quote from the Standard proving or disproving the necessity.

--

Frederick Gotham


Frederick Gotham:
Frederick Gotham:

基本上你会问,标准是否需要多维数组在内存中以特定的方式存在,

具体来说:


a [0] [0] a [0] [1] a [0] [2] a [1] [0] a [ 1] [1] a [1] [2]


我很确定事情必须像这样,但也许有人可以

引用标准证明或反驳必要性。
Basically you''re asking if the Standard necessitates that
multi-dimensional arrays be lain out in a particular way in memory,
specifically:

a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2]

I''m pretty sure that things have to be like this, but maybe someone can
give a quote from the Standard proving or disproving the necessity.



实际上,如果我对它有点想法,我不需要咨询

标准。 br />

我们知道:


(1)数组元素之间或之后不能填充。

(2) )每个连续的数组元素必须有升序地址。


此外,C中没有多维数组,而是
数组数组。因此,看第一个数组:


int(a [2])[3];


我们知道这三个元素这个数组不能在它们之间有填充,并且它们的地址必须是升序的。现在,如果我们看一下

数组类型,我们会看到每个元素都是一个int [2]。我们知道这个内部的
数组也必须在元素之间没有填充,并且

地址必须是提升的。


因此,上面显示的内存布局是必须的

标准。


-


弗雷德里克Gotham


I actually, if I put a tad bit of thought into it, I don''t need to consult
the Standard.

We know that:

(1) There cannot be padding between array elements or after.
(2) Each consecutive array element must have ascending addresses.

Also, there''s no such thing as multi-dimensional arrays in C, but rather
arrays of arrays. Therefore, looking at the first array:

int (a[2])[3];

We know that the three elements in this array must not have padding between
them, and that their addresses must be ascending. Now, if we look at the
array type, we see that each element is an int[2]. We know that this inner
array must also have no padding between elements, and also that the
addresses must be ascending.

Therefore, the memory layout I showed above is necessitated by the
Standard.

--

Frederick Gotham


这篇关于指向数组的指针 - 是否有超出限制的访问权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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