棘手的问题 [英] tricky question

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

问题描述

计划1:


#include< stdio.h>

int main(void){

int * p ;

p =(int *)malloc(sizeof(int));

* p = 12;

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

返回0;

}


我运行程序得到以下输出:


12 0x80495b8


现在


程序2:


#include< stdio.h>

int main(void){

int * p;

p = 0x80495b8;

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

返回0;

}


应该是什么答案如果,不知何故,执行

程序2的进程被允许访问进程的所有内存位置

执行代表计划1。


我认为它应该是12?如果我错了,请纠正我//


谢谢,

Onkar

解决方案

onkar写道:


程序1:


#include< stdio.h>



其中'是malloc的stdlib.h。你正在调用未定义的行为

已经。


int main(void){

int * p ;

p =(int *)malloc(sizeof(int));



无需转换malloc的返回值。


* p = 12;

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



将%p

说明符对应的参数转换成类型为void指针的形式更好。
< blockquote class =post_quotes>
返回0;

}


我运行程序得到以下输出:


12 0x80495b8


现在


计划2:


# include< stdio.h>

int main(void){

int * p;

p = 0x80495b8;



实现定义的行为。


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

返回0;

}


应该是什么答案如果,不知何故,进程执行

程序2被允许访问代表程序1执行的过程

的所有内存位置。


我认为它应该12岁?如果我错了,请纠正我//



好​​吧,你认为错了。第二个程序没有任何保证。

它会调用未定义的行为。


< OT>

在典型的现代操作系统中,每个进程都给它自己的

地址空间,因此进程二的地址0x80495b8不需要与进程一的地址相对应。事实上,他们不会将b $ b映射到相同的物理内存。如果他们这样做了,那么

操作系统就会被破坏,或者系统不支持虚拟内存,或者虚拟内存被禁用了。(br /虚拟内存已被禁用)。 >
< / OT>


" onkar" < on ******* @ gmail.comwrote in message

news:11 ********************** @ n33g2000cwc.googlegr oups.com ...


计划1:


#include< stdio.h>

int main(void){

int * p;

p =(int *)malloc(sizeof(int));

* p = 12;

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

返回0;

}


我运行程序得到以下输出:


12 0x80495b8


现在


计划2:


#include< stdio.h>

int main(void) {

int * p;

p = 0x80495b8;

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

返回0;

}


应该是什么答案如果,不知何故进程执行

程序2允许访问代表执行的进程

的所有内存位置计划1。


我认为它应该是12?如果我错了,请纠正我//



这是未定义的行为。在一些实现中,它可以按预期工作,

但是在大多数现代系统上,结果将是随机的,零b $ b或崩溃(例如段错误)。


S


-

Stephen Sprunk那些认为自己知道一切的人

CCIE#3723对我们这些人来说是一个很大的烦恼。

K5SSS --Isaac Asimov

-

通过 http://www.teranews.com 上的免费Usenet帐户发布


" santosh" < sa ********* @ gmail.comwrites:


onkar写道:



[...]


>程序2:

#include< stdio.h> ;
int main(void){
int * p;
p = 0x80495b8;



实现定义的行为。



不,这是违反约束条件。


> ; printf("%d \ n",* p);
返回0;
}



没有从整数到指针的隐式转换(除了

空指针常量的特殊情况)。 (有些编译器可能会将
允许作为扩展名,并且*可能*实现

隐式转换,但标准允许其他可能性。)


如果作业从

p = 0x80495b8更改;



p =(int *)0x80495b8;

然后转换的行为将是实现定义的。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *< http://users.sdsc.edu/~kst>

"我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长


Program 1:

#include<stdio.h>
int main(void){
int *p;
p=(int *)malloc(sizeof(int));
*p=12;
printf("%d %p\n",*p,p);
return 0;
}

I run the program to get the following output :

12 0x80495b8

now

Program 2:

#include<stdio.h>
int main(void){
int *p;
p=0x80495b8;
printf("%d\n",*p);
return 0;
}

what should be the answer "if ,somehow the Process executing the
program 2 is allowed to access all the memory locations of Process
executing on behalf of Program 1 ".

I think it should be 12 ? correct me if I am wrong //

Thank you,
Onkar

解决方案

onkar wrote:

Program 1:

#include<stdio.h>

Where''s stdlib.h for malloc. You''re invoking undefined behaviour
already.

int main(void){
int *p;
p=(int *)malloc(sizeof(int));

No need to cast the return value of malloc.

*p=12;
printf("%d %p\n",*p,p);

It''s better form to cast the argument corresponding to the %p
specifier to type void pointer.

return 0;
}

I run the program to get the following output :

12 0x80495b8

now

Program 2:

#include<stdio.h>
int main(void){
int *p;
p=0x80495b8;

Implementation defined behaviour.

printf("%d\n",*p);
return 0;
}

what should be the answer "if ,somehow the Process executing the
program 2 is allowed to access all the memory locations of Process
executing on behalf of Program 1 ".

I think it should be 12 ? correct me if I am wrong //

Well, you think wrong. Nothing about the second program is guaranteed.
It invokes undefined behaviour.

<OT>
In typical modern operating systems, each process is given it''s own
address space, so the address 0x80495b8 of process two need not
correspond with the same address of process one. In fact, they won''t
map to the same physical memory. If they did, then either the
operating system is broken or the system doesn''t support virtual
memory, (or virtual memory has been disabled).
</OT>


"onkar" <on*******@gmail.comwrote in message
news:11**********************@n33g2000cwc.googlegr oups.com...

Program 1:

#include<stdio.h>
int main(void){
int *p;
p=(int *)malloc(sizeof(int));
*p=12;
printf("%d %p\n",*p,p);
return 0;
}

I run the program to get the following output :

12 0x80495b8

now

Program 2:

#include<stdio.h>
int main(void){
int *p;
p=0x80495b8;
printf("%d\n",*p);
return 0;
}

what should be the answer "if ,somehow the Process executing the
program 2 is allowed to access all the memory locations of Process
executing on behalf of Program 1 ".

I think it should be 12 ? correct me if I am wrong //

It''s undefined behavior. On some implementations it may work as you expect,
but on the majority of modern systems the result will be either random,
zero, or a crash (e.g. segfault).

S

--
Stephen Sprunk "Those people who think they know everything
CCIE #3723 are a great annoyance to those of us who do."
K5SSS --Isaac Asimov
--
Posted via a free Usenet account from http://www.teranews.com


"santosh" <sa*********@gmail.comwrites:

onkar wrote:

[...]

>Program 2:

#include<stdio.h>
int main(void){
int *p;
p=0x80495b8;


Implementation defined behaviour.

No, it''s a constraint violation.

> printf("%d\n",*p);
return 0;
}

There is no implicit conversion from integers to pointers (other than
the special case of a null pointer constant). (Some compilers may
allow the assignment as an extension, and will *probably* implement an
implicit conversion, but the standard allows other possibilities.)

If the assignment were changed from
p=0x80495b8;
to
p=(int*)0x80495b8;
then the behavior of the conversion would be implementation-defined.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


这篇关于棘手的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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