请解释这个行为......, [英] Please Explain This behaviour...,

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

问题描述

代码正在编译而没有错误/错误/例外..,

这种行为有什么可能?

// ******** ****************************************** ******** *****

#include< stdio.h>


typedef struct abc_ * abc;


静态abc param;


abc fun(无效)

{

float * a = NULL;

a =(float *)malloc(sizeof(float));

if(a){

return((abc)(a));

}

返回NULL;

}


int main()

{

param = fun();

printf(" test_bandwidth_alloc:%x \ n",param);

free(param) ;

}

解决方案

9月5日上午9:50,Pranav< pranav ... @ gmail。编写:


代码编译没有错误/错误/异常..,

这种行为有什么可能性?

// ********** **************************************** ********** ***

#include< stdio.h>


typedef struct abc_ * abc;


static abc param;


abc fun(无效)

{

float * a = NULL;

a =(float *)malloc(sizeof(float));

if(a){

return((abc)(a));



a有类型(float *)。 abc是(struct abc_ *)的别名。

这两种类型可以有不同的表示和大小。


}

返回NULL;

}


int main()

{

param = fun();

printf(" test_bandwidth_alloc:%x \ n",param);



这里你传递一个(struct abc_ *),其中printf期望unsigned int,并且

你调用未定义的行为。
< blockquote class =post_quotes>
free(param);


}


On Sep 5,上午11:59,vipps ... @ gmail.com写道:


9月5日上午9:50,Pranav< pranav ... @ gmail.com写道:


代码编译没有错误/错误/异常..,

这种行为有什么可能?

// ***************************************** ********* *************

#include< stdio.h>


typedef struct abc_ * abc;


static abc param;


abc fun(void)

{

float * a = NULL;

a =(float *)malloc(sizeof(float));

if(a){

return((abc)(a));



a有类型(float *)。 abc是(struct abc_ *)的别名。

这两种类型可以有不同的表示和大小。


}

返回NULL;

}


int main()

{

param = fun();

printf(" test_bandwidth_alloc:%x \ n",param);



这里你传递一个(struct abc_ *),其中printf期望unsigned int,并且

你调用未定义的行为。
< blockquote class =post_quotes>
free(param);


}



但是没有定义结构(abc)在整个代码中它是什么呢?b $ b是别名?


Pranav说:


代码编译没有错误/错误/例外..,

这种行为有什么可能?

// ********* ***************************************** ********* ****

#include< stdio.h>


typedef struct abc_ * abc;


static abc param;


abc fun(无效)

{

float * a = NULL;

a =(float *)malloc(sizeof(float));



因为你未能#include< stdlib.hyou未能为malloc提供

声明。


因为没有malloc的声明,编译器有义务

假设malloc返回int,即使我们都知道它确实返回

void *。


实现可以合法地生成代码,这些代码可以从不同于寄存器返回值的寄存器中检索int

返回值。所以,例如,
,malloc可以将它的指针放在给定的寄存器中,但是

调用代码 - 在我们知道它生成的假的但是强制性的

假设malloc返回int - 可以从

不同的寄存器中获取值,结果非常热闹。 (这只是一个场景

这里的问题怎么会出错 - 还有其他的。)


由于这种危险,编译器有义务当你搞砸了类型时发出诊断

消息 - 但如果你明确坚持

(通过强制转换)编译器应该这样做,那么就有义务警告

你被删除了。


从现在开始,代码的行为是未定义的,任何东西都可以发生
。这可能就是为什么你得到了你b $ b观察到的行为。或者,当然,它可能不会。这就是令人兴奋的

未定义的行为。


-

Richard Heathfield< http:// www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日


The Code is compiling without Error/Bug/Exception..,
What are the possibilities for this behaviour?
//************************************************** *************
#include<stdio.h>

typedef struct abc_ *abc;

static abc param;

abc fun(void)
{
float *a = NULL;
a = (float *)malloc(sizeof(float));
if (a) {
return ((abc)(a));
}
return NULL;
}

int main()
{
param = fun();
printf("test_bandwidth_alloc: %x\n", param);
free(param);
}

解决方案

On Sep 5, 9:50 am, Pranav <pranav...@gmail.comwrote:

The Code is compiling without Error/Bug/Exception..,
What are the possibilities for this behaviour?
//************************************************** *************
#include<stdio.h>

typedef struct abc_ *abc;

static abc param;

abc fun(void)
{
float *a = NULL;
a = (float *)malloc(sizeof(float));
if (a) {
return ((abc)(a));

a has type (float *). abc is alias for (struct abc_ *).
These two types can have different representation and size.

}
return NULL;
}

int main()
{
param = fun();
printf("test_bandwidth_alloc: %x\n", param);

Here you pass a (struct abc_ *) where printf expects unsigned int, and
you invoke undefined behavior.

free(param);

}


On Sep 5, 11:59 am, vipps...@gmail.com wrote:

On Sep 5, 9:50 am, Pranav <pranav...@gmail.comwrote:

The Code is compiling without Error/Bug/Exception..,
What are the possibilities for this behaviour?
//************************************************** *************
#include<stdio.h>

typedef struct abc_ *abc;

static abc param;

abc fun(void)
{
float *a = NULL;
a = (float *)malloc(sizeof(float));
if (a) {
return ((abc)(a));


a has type (float *). abc is alias for (struct abc_ *).
These two types can have different representation and size.

}
return NULL;
}

int main()
{
param = fun();
printf("test_bandwidth_alloc: %x\n", param);


Here you pass a (struct abc_ *) where printf expects unsigned int, and
you invoke undefined behavior.

free(param);

}

But there is no structure(abc) defined in the whole code then what it
is aliasing?


Pranav said:

The Code is compiling without Error/Bug/Exception..,
What are the possibilities for this behaviour?
//************************************************** *************
#include<stdio.h>

typedef struct abc_ *abc;

static abc param;

abc fun(void)
{
float *a = NULL;
a = (float *)malloc(sizeof(float));

Because you failed to #include <stdlib.hyou have failed to provide a
declaration for malloc.

Because there is no declaration for malloc, the compiler is obliged to
assume that malloc returns int, even though we all know it really returns
void *.

Implementations can legitimately generate code that can retrieve an int
return value from a different register than a pointer return value. So,
for example, malloc could place its pointer in a given register, but the
calling code - generated on the we-know-it''s-false but obligatory
assumption that malloc returns int - could fetch the value from a
different register, with hilarious results. (That''s just one scenario of
how things can go wrong here - there are others.)

Because of this danger, the compiler is obliged to issue a diagnostic
message when you screw up types this badly - but if you explicitly insist
(via the cast) that the compiler should just do it, the obligation to warn
you is removed.

From this point on, the behaviour of the code is undefined, and anything
can happen. And that may well be why you are getting the behaviour you
observe. Or, of course, it may not. That''s what is so exciting about
undefined behaviour.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


这篇关于请解释这个行为......,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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