为什么编译器不生成任何警告? [英] Why compiler not generating any warning ?

查看:96
本文介绍了为什么编译器不生成任何警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑下面这段代码:


struct junk {

int i_val;

int i_val1;

char c_val;

};


int main(无效)

{

struct junk str_junk;

int * i_ptr;

struct junk * pstr_junk;

pstr_junk =& str_junk;

i_ptr =(int *)pstr_junk + 1; / *不应该编译器

在这里发出警告? * /

}


编译此程序时:cc -c99 -check -portable test.c

我不喜欢得到任何警告。

然而,声明

i_ptr =(int *)pstr_junk + 1;对我来说似乎是不对的。


这里,pstr_junk是一个指向结构的指针,它被强制转换为

" int *"这可能导致未定义的行为。

但是在编译时我没有收到任何警告信息。

那么,有什么方法可以解决这些类型的错误?有没有

任何其他工具可能会发现所有未定义的,不可移植的

行为?

我也试过* lint *但它也没有我没有警告过这个。


只有,对char *进行类型转换。应允许

*保证*指向结构的最低寻址字节。

解决方案

< blockquote> ju**********@yahoo.co.in 写道:

但是在编译时我没有收到任何警告信息。
那么,有什么方法可以解决这些类型的错误?



了解更多C.


-

pete


ju**********@yahoo.co.in 写道:

考虑以下代码:
struct junk {
int i_val;
int i_val1;
char c_val;
};
int main(void)
{struct junk str_junk;
int * i_ptr;
struct junk * pstr_junk;
pstr_junk =& str_junk;
i_ptr =(int *)pstr_junk + 1; / *不应该编译器在这里发出警告吗? * /
}
在编译这个程序时:cc -c99 -check -portable test.c
我没有得到任何警告。
然而,声明 i_ptr =(int *)pstr_junk + 1;似乎对我不正确。
这里,pstr_junk是一个指向结构的指针,它被强制转换为
int *这可能会导致未定义的行为。


如果您使用指针已经解除分配的内存等,当您在数组结束时写入时,您还会得到未定义的行为。 br />
但是编译器不会对此发出警告。

但是在编译时我没有收到任何警告信息。


好​​吧,你告诉编译器将这个指针转换为

指针的另一种类型。所以编译器会这样做,并且因为你明确地说b $ b告诉它这样做,为什么它会怀疑你命令的智慧?

如果你施展你基本上说我知道我在做什么,不要告诉

我不应该,如果我希望你警告我,我就不会用它

[expletive]审查]演员。然后你告诉它分配给

指针来自同一类型的指针。这是100%合法和

完全没有危险。

那么,有什么方法可以解决这些类型的错误?是否有任何其他工具可以找出所有未定义的,不可移植的行为?


最好的工具可能在你的耳朵之间;-)查看所有演员表格

可疑,直到你满意他们没事。查找

所有未定义行为的实例都是不可能的,它可能只在运行时发生
- 编译器通常无法知道ad-
$ b例如,$ b vance在对数组元素的访问中,索引将在数组的范围内变为
。未定义的行为是

本身并不是邪恶的东西,它只是C标准

没有定义的东西。在大多数程序中,您实际上依赖于未经处理的行为,以确定合理的行为,例如:当你在第三方库中调用一个函数

时 - 一个严格符合的程序可能

只使用
$中指定的语言和库的那些功能b $ b C标准你经常需要的不仅仅是那个。

只有,对char *进行类型转换。应该被允许
*保证*指向结构的最低寻址字节。




在这种情况下可能是的。但是在某种情况下你可以知道你正在使用的平台会发生什么事情

(因为它有详细记录)并且你必须明确地说明

调用未定义的行为以完成任务。


问候,Jens

-

\ Jens Thoms Toerring ___ Je ******* ****@physik.fu-berlin.de

\ __________________________ http://www.toerring.de


pete写道:

ju ********** @ yahoo.co.in 写道:

< blockquote class =post_quotes>但是在编译时我没有收到任何警告信息。
那么,有什么方法可以解决这些类型的错误?



学习更多C.



这个回复有什么意义?


他正在努力学习更多C并在这个

讨论组中提出一个问题。


当然*你*你在学习C时从未问过任何问题。


为什么对新人很粗鲁?


如果你不想回答或者你认为问题很愚蠢

只是不回复。


jakob


Consider the following piece of code:

struct junk {
int i_val;
int i_val1;
char c_val;
};

int main(void)
{
struct junk str_junk;
int * i_ptr;
struct junk * pstr_junk;
pstr_junk = &str_junk;
i_ptr = (int *)pstr_junk + 1; /* shouldn''t the compiler
give warning here ? */
}

On compiling this program: cc -c99 -check -portable test.c
I don''t get any warning.
However, the statement
i_ptr = (int *)pstr_junk + 1; seems to be incorrect to me.

Here, pstr_junk is a pointer to structure and it is being typecasted to
"int *" which may cause undefined behaviour.
But on compilation I don''t get any warning message.
So, what is the way to cacth these type of bugs ? Is there
any other tool that may find out all undefined, unportable
behaviour ?
I tried *lint* as well but it also didn''t warned for this.

Only, the typecasting to "char *" should be allowed which is
*guaranteed* to point to the lowest addressed byte of the structure.

解决方案

ju**********@yahoo.co.in wrote:

But on compilation I don''t get any warning message.
So, what is the way to cacth these type of bugs ?



Learn more C.

--
pete


ju**********@yahoo.co.in wrote:

Consider the following piece of code: struct junk {
int i_val;
int i_val1;
char c_val;
}; int main(void)
{
struct junk str_junk;
int * i_ptr;
struct junk * pstr_junk;
pstr_junk = &str_junk;
i_ptr = (int *)pstr_junk + 1; /* shouldn''t the compiler
give warning here ? */
} On compiling this program: cc -c99 -check -portable test.c
I don''t get any warning.
However, the statement
i_ptr = (int *)pstr_junk + 1; seems to be incorrect to me. Here, pstr_junk is a pointer to structure and it is being typecasted to
"int *" which may cause undefined behaviour.
You will also get undefined behaviour when you write past the end of
an array, if you use a pointer to already deallocated memory etc. ect.
but the compiler won''t warn about that.
But on compilation I don''t get any warning message.
Well, you tell the compiler "convert this pointer to a pointer of
a different type". So the compiler does, and since you explicitely
told it to do that why should it doubt the wisdom of your commands?
If you cast you basically say "I know what I am doing, don''t tell
me I shouldn''t, if I would like you to warn me I wouldn''t use that
[expletive censored] cast". And afterwards you tell it "assign to
a pointer from a pointer of the same type" which is 100% legal and
not dangerous at all.
So, what is the way to cacth these type of bugs ? Is there
any other tool that may find out all undefined, unportable
behaviour ?
The best tool is probably between your ears;-) View all casts as
being suspicious until you''re satisifed that they are ok. Finding
all instances of undefined behaviour would be impossible, it might
only happen at runtime - the compiler can''t typically know in ad-
vance if e.g. in an access of an array element the index is going
to be within the bounds of the array. And undefined behaviour is
not something evil in itself, it''s just something the C standard
does not define. And in most programs you actually rely on unde-
fined behaviour to work out fine, e.g. when you call a function
in a third-party library - while a strictly conforming program may
use only those features of the language and library specified in
the C standard you very often need more than just that.
Only, the typecasting to "char *" should be allowed which is
*guaranteed* to point to the lowest addressed byte of the structure.



In this case maybe yes. But there can be situations where you
know what''s going to happen on the platform you''re working on
(because it''s well-documented) and where you have to explicitely
invoke undefined behaviour in order to get things done.

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de


pete wrote:

ju**********@yahoo.co.in wrote:

But on compilation I don''t get any warning message.
So, what is the way to cacth these type of bugs ?


Learn more C.


What is the point of this reply?

He is exactly trying to learn more C and asks in this
discussion group a question.

Of course *you* never asked any questions when you were learning C.

Why being rude to newcomers?

If you do not feel like answering or you think the question is silly
just do not reply.

jakob


这篇关于为什么编译器不生成任何警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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