且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

小C“拼图”

更新时间:2023-11-20 15:24:46

Robert Gamble< ro ********** @ hotmail.com>写道:
Robert Gamble <ro**********@hotmail.com> wrote:
我正在看一些C谜题:
http://purana.csa.iisc.ernet.in/~gkumar/cquestions.html 并没有任何问题其中第一个被复制的除了以下内容:

以下C程序的IA-64段错误,但在IA-32上工作正常。
int main()
{
int * p;
p =(int *)malloc(sizeof(int));
* p = 10;
返回0;
}
为什么会这样?
I was taking a look at some of the C puzzles at:
http://purana.csa.iisc.ernet.in/~gkumar/cquestions.html and have not had
any trouble with any of them except for the first one which is reproduced
below:

The following C program segfaults of IA-64, but works fine on IA-32.
int main()
{
int* p;
p = (int*)malloc(sizeof(int));
*p = 10;
return 0;
}
Why does it happen so?




您是否检查过以确保p!= NULL?无论出于什么原因,可能有一个malloc()失败




-

Kristofer Pettijohn
kr ******* @ cybernetik.net


Robert Gamble< ro ********** @ hotmail.com>写道:
Robert Gamble <ro**********@hotmail.com> writes:
我正在看一些C谜题:
http://purana.csa.iisc.ernet.in/~gkumar/cquestions.html 并没有任何问题其中第一个被复制的除了以下内容:

以下C程序的IA-64段错误,但在IA-32上工作正常。
int main()
{
int * p;
p =(int *)malloc(sizeof(int));
* p = 10;
返回0;
}
为什么会这样?

现在我知道依赖于架构的行为是偏离主题的,但我并没有真正看到代码有什么问题(是的,缺少stdlib.h,int
main(void)和malloc已经投入,但我不认为这有任何事情可以解决问题。我在这里错过了什么吗?我不能访问IA-64机器,任何有洞察力的人?
I was taking a look at some of the C puzzles at:
http://purana.csa.iisc.ernet.in/~gkumar/cquestions.html and have not had
any trouble with any of them except for the first one which is reproduced
below:

The following C program segfaults of IA-64, but works fine on IA-32.
int main()
{
int* p;
p = (int*)malloc(sizeof(int));
*p = 10;
return 0;
}
Why does it happen so?

Now I know that architecture dependant behavior is off-topic, but I don''t
really see anything wrong with the code (yes, stdlib.h is missing, int
main(void), and malloc casted, but I don''t think this has anything to do
with the point of the problem). Am I missing something here? I don''t
have access to an IA-64 machine, anyone who does have any insight?




缺少的#include< stdlib .H&GT;&QUOT;确实是问题。


如果没有malloc()的可见原型,编译器

假定它返回int。由于int是32位,但malloc()*尝试*返回的实际值是64位,结果是

未定义的行为。在这种情况下,未定义的行为表示

本身返回指针的低位(我认为)32位

值。由于高位包括非零值,因此输掉了
信息。转换为int *会掩盖错误,并生成一个

无效指针值(其上半部分恰好是全位零)。

取消引用指针上的指针值下一行导致更多未定义的

行为,这表现为seg故障。


它恰好在IA-32上工作(通常称为x86)因为int

和指针类型都是32位。短语恰好工作

意味着未定义的行为表现为程序

做你期望它做的事情。 (这实际上比IA-64上的
更糟糕,因为它掩盖了错误。)


-

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

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

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



The missing "#include <stdlib.h>" is exactly the problem.

In the absence of a visible prototype for malloc(), the compiler
assumes that it returns int. Since int is 32 bits, but the actual
void* value that malloc() *tries* to return is 64 bits, the result is
undefined behavior. In this case, the undefined behavior manifests
itself as returning the low-order (I think) 32 bits of the pointer
value. Since the high-order bits include non-zero values, this loses
information. Casting to int* masks the error, and generates an
invalid pointer value (whose upper half happens to be all-bits-zero).
Dereferencing the pointer value on the next line causes more undefined
behavior, which manifests itself as a seg fault.

It happens to work on IA-32 (more commonly known as x86) because int
and pointer types are both 32 bits. The phrase "happens to work"
means that the undefined behavior manifests itself as the program
doing what you expected it to do. (This is actually worse than what
happens on the IA-64, since it masks the error.)

--
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.


Robert Gamble写道:
Robert Gamble wrote:

我正在看一些C谜题:
http://purana.csa。 iisc.ernet.in/~gkumar/cquestions.html 并且除了下面转载的第一个
之外,没有任何问题。

int main()
{*> int * p;
p =(int * )malloc(sizeof(int));
* p = 10;
返回0;
}
为什么会这样?

现在我知道架构依赖的行为是偏离主题的,但是我并没有真正看到代码有什么问题(是的,
stdlib.h缺失,int main(void)和malloc铸造,但是我不认为这与
问题的关系有什么关系。我在这里错过了什么吗?我无法访问IA-64机器,任何有洞察力的人?

I was taking a look at some of the C puzzles at:
http://purana.csa.iisc.ernet.in/~gkumar/cquestions.html and have
not had any trouble with any of them except for the first one
which is reproduced below:

The following C program segfaults of IA-64, but works fine on IA-32.
int main()
{
int* p;
p = (int*)malloc(sizeof(int));
*p = 10;
return 0;
}
Why does it happen so?

Now I know that architecture dependant behavior is off-topic,
but I don''t really see anything wrong with the code (yes,
stdlib.h is missing, int main(void), and malloc casted, but I
don''t think this has anything to do with the point of the
problem). Am I missing something here? I don''t have access
to an IA-64 machine, anyone who does have any insight?




是的。删除不必要的强制转换,这已经阻止了

编译器警告,并且#include< stdlib.h>,并且问题应该是

消失。


-

答:因为它会破坏人们通常阅读文本的顺序。

问:为什么***发布这么糟糕的事情?

A:热门发布。

问:usenet和电子邮件中最烦人的是什么?



Yes it does. Remove the unnecessary cast, which has prevented
compiler warnings, and #include <stdlib.h>, and the problem should
go away.

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?