代码审查...... [英] Code Review...

查看:61
本文介绍了代码审查......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我邀请以下代码的评论:


#include< stdio.h>

#include< string.h>

#include< stdlib.h>


int

main(无效)

{

char * p;


p =(char *)& p;

strcpy(p," Hi");

printf("%s \ n",p);

返回EXIT_SUCCESS;

}

谢谢。


-

Vijay Kumar R Zanvar

我的家页面 - http://www.geocities.com/vijoeyz/

推荐答案

2003年12月24日星期三11:45:50 +0530,Vijay Kumar R Zanvar

< vi *****@hotpop.com>在comp.lang.c中写道:
On Wed, 24 Dec 2003 11:45:50 +0530, "Vijay Kumar R Zanvar"
<vi*****@hotpop.com> wrote in comp.lang.c:


我邀请以下代码的评论:


您的代码调用未定义的行为。

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

int
main(无效)
{
char * p;


p是一个未初始化的char指针。

p =(char *)& p;


p现在包含自己的地址。

strcpy(p," Hi");


现在你用三个字符覆盖p'的内容,'''',''我'和

''\ 0 ''。如果sizeof(char *)是<立即未定义的行为3,在许多16位实现中,
是正确的。

printf("%s \ n",p);


未定义的行为当然,您已经通过

左值的字符类型修改了p的值。将它作为指针访问,或者实际上作为一个字符类型数组以外的其他任何东西,现在都是未定义的

行为。


未定义的行为也是因为printf()会尝试取消引用

p,这几乎肯定不再指向你的程序字符串

有权访问。

返回EXIT_SUCCESS;
}

谢谢。
Hi,

I invite reviews for the following code:
Your code invokes undefined behavior.

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

int
main ( void )
{
char *p;
p is an uninitialized pointer to char.
p = (char*) &p;
p now contains the its own address.
strcpy ( p, "Hi" );
Now you overwrite p''s contents with three characters, ''H'', ''i'', and
''\0''. Immediate undefined behavior if sizeof (char *) is < 3, which
is true on many 16-bit implementations.
printf ( "%s\n", p );
Undefined behavior for sure, you have modified the value of p via an
lvalue of character type. Accessing it as a pointer, or indeed as
anything other than an array of character type, is now undefined
behavior.

Undefined behavior also because printf() will attempt to dereference
p, which almost certainly no longer points to a string your program
has the right to access.
return EXIT_SUCCESS;
}
Thanks.




你实际上认为这个愚蠢的废话有什么好处?


-

Jack Klein

主页: http://JK-Technology.Com

常见问题解答

comp.lang.c http://www.eskimo.com/~scs/C-faq /top.html

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt。 comp.lang.learn.c-c ++ ftp:// snurse -l .org / pub / acllc-c ++ / faq



What did you actually think this silly nonsense would be good for?

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq


" Vijay Kumar R Zanvar" < 6 ***** @ hotpop.com>在留言中写道

news:bs ************ @ ID-203837.news.uni-berlin.de ...
"Vijay Kumar R Zanvar" <vi*****@hotpop.com> wrote in message
news:bs************@ID-203837.news.uni-berlin.de...
你好,

我邀请评论以下代码:

#include< stdio.h>
#include< string.h>
#包括< stdlib.h>
包括确定。
int
main(无效)
{*> char * p;

p =(char *)& p ;
为什么将指针地址转换为指针?当你操作指针时,

* p会让你访问存储在指针地址的内容。

类似于普通变量:


int p = 5

printf("%d \ n",p);将产生5


eq

int * p = 5;


printf(" %d \ n,* p);也将产生5美元


printf("%d \ n",p);将产生存储p的内存地址。


执行此转换将始终正确编译,但产生一个seg。错误。

strcpy(p," Hi");
printf("%s \ n",p);
返回EXIT_SUCCESS;


假设EXIT_SUCCESS为0(简单地放入''定义EXIT_SUCCESS 0'')}

谢谢。

- -
Vijay Kumar R Zanvar
我的主页 - http:// www.geocities.com/vijoeyz/
Hi,

I invite reviews for the following code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h> Includes ok.
int
main ( void )
{
char *p;

p = (char*) &p; Why cast the pointers address to the pointer? WHen you operate on pointers,
*p will give you accessto what is stored at the pointers address.
Similar to ordinary variables:

int p=5

printf ( "%d\n", p ); will yield 5

eq

int *p = 5;

printf ( "%d\n", *p ); will yield 5 also

printf ( "%d\n", p ); will yield the address in memory where p is stored.

Doing this cast will as always compile correctly, but yield a seg. fault.
strcpy ( p, "Hi" );
printf ( "%s\n", p );
return EXIT_SUCCESS;
Assuming that EXIT_SUCCESS is 0 (simply put in a ''define EXIT_SUCCESS 0'') }
Thanks.

--
Vijay Kumar R Zanvar
My Home Page - http://www.geocities.com/vijoeyz/




-


我希望这个在你想要的答案附近。


Ronny Mandal



--

I hope that this was nearby the answer you wished for.

Ronny Mandal


" Ronny Mandal" < RO ***** @ math.uio.no>写道:
"Ronny Mandal" <ro*****@math.uio.no> wrote:
当您操作指针时,* p将允许您访问指针地址中存储的内容。与普通变量类似:

int p = 5
;

printf("%d \ n",p);将产生5


它将输出数字5和换行符,是的。

eq

int * p = 5;


这错误地尝试用整数初始化指针类型。它是
违反约束,因此编译器必须发出诊断

消息。也许你的意思是:

int i = 5;

int * p =& i;

现在我的值为5, p具有i的地址值。

printf("%d \ n",* p);将产生5美元也是


如果我的更正,则为真。

printf("%d \ nn,p);将产生存储p的存储器中的地址。


这是未定义的行为,因为%d转换需要int作为其

参数。输出

指针值的表示的正确方法是:

printf("%p \ n",(void *)p);

这会将''指针指向int''的值转换为类型值

''指向void''的指针,如%p转换说明符所要求的那样。 br />
假设EXIT_SUCCESS为0(简单地放入''define EXIT_SUCCESS 0'')
When you operate on pointers, *p will give you access to what is
stored at the pointers address. Similar to ordinary variables:

int p=5 ;
printf ( "%d\n", p ); will yield 5
It''ll output the digit 5 and a newline character, yeah.
eq

int *p = 5;
This wrongly attempts to initialise a pointer type with an integer. It
is a constraint violation, so the compiler must emit a diagnostic
message. Perhaps you actually meant:
int i = 5;
int *p = &i;
Now i has the value 5, and p has the value of the address of i.
printf ( "%d\n", *p ); will yield 5 also
True, given my correction.
printf ( "%d\n", p ); will yield the address in memory where p is stored.
This is undefined behaviour, as the %d conversion requires an int as its
argument. The correct way to output a representation of the value of a
pointer is:
printf("%p\n", (void *)p);
This converts the value of type ''pointer to int'' into a value of type
''pointer to void'' as required by the %p conversion specifier.
Assuming that EXIT_SUCCESS is 0 (simply put in a ''define EXIT_SUCCESS 0'')




不! EXIT_SUCCESS是在< stdlib.h>中定义的宏,其中正确包含了OP Vijay

。它与返回0具有相同的含义,但是需要

实际上没有值0.您不能自己定义这个

宏,这将是未定义的行为。


-

Simon。



No! EXIT_SUCCESS is a macro defined in <stdlib.h>, which the OP Vijay
correctly included. It has the same meaning as returning 0, but need
not actually have the value 0. You are not allowed to define this
macro yourself, that would be undefined behaviour.

--
Simon.


这篇关于代码审查......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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