如何修复编译器警告 [英] How to fix compiler warning

查看:95
本文介绍了如何修复编译器警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个宏,我全面使用它来释放ram。我想

清理我的代码所以我不会收到这些警告。


#define sfree(x)_internal_sfree((void **)& x)

#define _internal_sfree(x)({if(x&& * x){free(* x); * x = NULL;}})


void main(){

char * x =(char *)malloc(10);

int * y =(int *)malloc(10);


sfree(x);

sfree(y);

}


导致:


警告:解除引用类型惩罚指针会打破严格别名

规则

解决方案

Dave Stafford写道:


我有一个宏,我全面使用释放ram。我想

清理我的代码所以我不会收到这些警告。


#define sfree(x)_internal_sfree((void **)& x)

#define _internal_sfree(x)({if(x&& * x){free(* x); * x = NULL;}})



删除表达式周围的无关括号。


void main(){


如果您没有收到此警告,请提高警告级别!


-

Ian柯林斯。


Ian Collins写道:


Dave Stafford写道:


>我有一个宏,我全面使用它来释放ram。我想清理我的代码,所以我不会收到这些警告。

#define sfree(x)_internal_sfree((void **)& x)
#define _internal_sfree(x)({if(x&& * x){free(* x); * x = NULL;}})



删除表达式周围的无关括号。



这仍然留下了为什么要转换为void **以及为什么要测试NULL?


怎么样


#define sfree(x)_internal_sfree(& x)

#define _internal_sfree (x){free(* x); * X = NULL; }


-

Ian Collins。


Dave Stafford< in **** *@in.validwrites:


我有一个宏,我全面使用它来释放ram。我想

清理我的代码所以我不会收到这些警告。


#define sfree(x)_internal_sfree((void **)& x)

#define _internal_sfree(x)({if(x&& * x){free(* x); * x = NULL;}})


void main(){

char * x =(char *)malloc(10);

int * y =(int *)malloc(10);


sfree(x);

sfree(y);

}


导致:


警告:解除引用类型惩罚指针会破坏严格别名

规则



您的宏取决于特定于gcc的扩展(请参阅gcc手册中的Statement Exprs

)。


你要注意避免将空指针传递给free(),但

free(NULL)保证什么都不做。


看一看在您的程序的以下版本。

================================

#include< stdlib.h>


#define SFREE(p)(free(p),(p)= NULL)


int main(void){

char * x = malloc(10) ;

int * y = malloc(10 * sizeof * y);


SFREE(x);

SFREE(y );

返回0;

}

==================== ============


我所做的每一项更改都修复了代码中的错误:


main()返回int,而不是void。因为它返回int,你应该

返回一个int。


我从'sfree'重命名了宏。到SFREE;按照惯例,大多数

宏应该具有全大写字母名称。


在宏定义中,对参数的引用应该包含在
$ b中$ b括号以避免运算符优先级问题。


转换malloc()的结果是没用的,可以隐藏错误。


你没有没有''#include< stdlib.h>''。如果您要调用malloc(),那么这是必需的。演员阵容可能会使你的编译器对这个问题发出警告,但是并没有解决这个问题(这就像把警告线上的电线剪掉了一样)在你汽车的仪表板上。


为int *分配10个字节,如果,对于

例如,sizeof( int)== 4.我将其更改为分配10个整数。


推荐阅读:comp.lang.c FAQ,< http://www.c-faq.com /> ;.


-

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

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

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

- Antony Jay和Jonathan Lynn,是部长


I have a macro that I use across the board for freeing ram. I''d like to
clean up my code so I don''t get these warnings.

#define sfree(x) _internal_sfree((void **)&x)
#define _internal_sfree(x) ({ if(x && *x) { free(*x); *x=NULL; } })

void main() {
char *x = (char *) malloc(10);
int *y = (int *) malloc(10);

sfree(x);
sfree(y);
}

results in:

warning: dereferencing type-punned pointer will break strict-aliasing
rules

解决方案

Dave Stafford wrote:

I have a macro that I use across the board for freeing ram. I''d like to
clean up my code so I don''t get these warnings.

#define sfree(x) _internal_sfree((void **)&x)
#define _internal_sfree(x) ({ if(x && *x) { free(*x); *x=NULL; } })

Remove the extraneous parenthesis around the expression.

void main() {

If you didn''t get a warning for this, crank up the warning level!

--
Ian Collins.


Ian Collins wrote:

Dave Stafford wrote:

>I have a macro that I use across the board for freeing ram. I''d like to
clean up my code so I don''t get these warnings.

#define sfree(x) _internal_sfree((void **)&x)
#define _internal_sfree(x) ({ if(x && *x) { free(*x); *x=NULL; } })

Remove the extraneous parenthesis around the expression.

Which still leaves the question why cast to void** and why test for NULL?

How about:

#define sfree(x) _internal_sfree(&x)
#define _internal_sfree(x) { free(*x); *x=NULL; }

--
Ian Collins.


Dave Stafford <in*****@in.validwrites:

I have a macro that I use across the board for freeing ram. I''d like to
clean up my code so I don''t get these warnings.

#define sfree(x) _internal_sfree((void **)&x)
#define _internal_sfree(x) ({ if(x && *x) { free(*x); *x=NULL; } })

void main() {
char *x = (char *) malloc(10);
int *y = (int *) malloc(10);

sfree(x);
sfree(y);
}

results in:

warning: dereferencing type-punned pointer will break strict-aliasing
rules

Your macro depends on a gcc-specific extension (see "Statement Exprs"
in the gcc manual).

You take care to avoid passing a null pointer to free(), but
free(NULL) is guaranteed to do nothing.

Take a look at the following version of your program.
================================
#include <stdlib.h>

#define SFREE(p) (free(p), (p) = NULL)

int main(void) {
char *x = malloc(10);
int *y = malloc(10 * sizeof *y);

SFREE(x);
SFREE(y);
return 0;
}
================================

Every change I made fixes a bug in your code:

main() returns int, not void. And since it returns int, you should
return an int.

I renamed the macro from "sfree" to "SFREE"; by convention, most
macros should have all-caps names.

In a macro definition, references to arguments should be enclosed in
parentheses to avoid operator precedence problems.

Casting the result of malloc() is useless, and can hide bugs.

You didn''t have a ''#include <stdlib.h>''. This is required if you''re
going to call malloc(). The casts probably silenced your compiler''s
warning about this, but didn''t fix the bug (it''s like snipping the
wire to a warning light on your car''s dashboard).

Allocating 10 bytes for an int* doesn''t make much sense if, for
example, sizeof(int) == 4. I changed it to allocate 10 ints.

Recommended reading: the comp.lang.c FAQ, <http://www.c-faq.com/>.

--
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天全站免登陆