使用calloc / malloc时有助于警告 [英] Help with warning when using calloc/malloc

查看:77
本文介绍了使用calloc / malloc时有助于警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手。请帮忙。以下警告由gcc-3.2.2

编译器(pc Linux)发出:

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

read_raw_data.c:51:警告:赋值使得整数指针

没有演员

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


编译下面的代码时。违规声明

是calloc。 main()函数中的类似语句没有任何

警告。我只允许在main()或

内使用calloc(和malloc)我做的事情非常愚蠢吗?

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

init(char *文件,双* grid,\

double * start){

double * depth;

int pts

depth = calloc(pts,sizeof(double));


}

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

谢谢。


MK

I am a newbie. Please help. The following warning is issued by gcc-3.2.2
compiler (pc Linux):
================================================== ================
read_raw_data.c:51: warning: assignment makes pointer from integer
without a cast
================================================== ================

when the following piece of code was compiled. The offending statement
is calloc. A similar statement in the main() function passes without any
warning. Am I allowed to use calloc (and malloc) only within main() or
am I doing something very silly ??
=============================================
init(char *file, double *grid, \
double *start){
double *depth;
int pts
depth = calloc(pts,sizeof (double));

}
============================================

Thanks.

MK

推荐答案



" MK" < MS *** @ yahoo.co.uk>在留言中写道

news:c0 ********** @ aspen.sucs.soton.ac.uk ...

"MK" <ms***@yahoo.co.uk> wrote in message
news:c0**********@aspen.sucs.soton.ac.uk...
我是新手。请帮忙。以下警告由gcc-3.2.2
编译器(pc Linux)发出:
========================= ========================= ================
read_raw_data.c:51 :警告:赋值使整数指针
没有演员
=============================== =================== ================

当下面的代码是编译。违规声明
是calloc。 main()函数中的类似语句没有任何
警告。我是否允许在main()或
中使用calloc(和malloc)我做的事情非常愚蠢?
================== ===========================
init(char *文件,double * grid,\
double * start ){

double * depth;
int pts
depth = calloc(pts,sizeof(double));

}
I am a newbie. Please help. The following warning is issued by gcc-3.2.2
compiler (pc Linux):
================================================== ================
read_raw_data.c:51: warning: assignment makes pointer from integer
without a cast
================================================== ================

when the following piece of code was compiled. The offending statement
is calloc. A similar statement in the main() function passes without any
warning. Am I allowed to use calloc (and malloc) only within main() or
am I doing something very silly ??
=============================================
init(char *file, double *grid, \
double *start){
double *depth;
int pts
depth = calloc(pts,sizeof (double));

}




您是否包括stdlib.h?


Tom



Did you include stdlib.h?

Tom


MK,


1.您没有使用值初始化pts

2.您应该将calloc返回的(void *)转换为(double *)

3.当声明指针时,将它们初始化为NULL是个好主意

开始(像free()这样的内存函数会忽略NULL')


请尝试以下代码...


init(char * file,double * grid,double * start){

double * depth = NULL; / *将所有

指针初始化为NULL * /

int pts = 5总是一个好主意; / *我们需要ptr初始化值* /

depth =(double *)calloc(pts,sizeof(double)); / *我们真的应该把

的结果投到(双*)* /

}


我还没有在GCC上尝试了这个代码,但它在VS2003中编译得很好。


祝你好运。


" MK" < MS *** @ yahoo.co.uk>在留言中写道

news:c0 ********** @ aspen.sucs.soton.ac.uk ...
MK,

1. You are not initialising pts with a value
2. You should cast the (void *) returned from calloc to (double *)
3. When declaring pointers it''s a good idea to initialise them to NULL to
start with (memory functions like free() will ignore NULL''s)

Try the following instead...

init(char *file, double *grid, double *start) {
double *depth = NULL; /* It''s always a good idea to initialise ALL
pointers to NULL */
int pts = 5; /* We need ptr to be initialise with a value */
depth = (double *) calloc(pts,sizeof (double)); /* We really should cast
the result to a (double *) */
}

I haven''t tried this code on GCC but it compiles fine in VS2003.

Good luck.

"MK" <ms***@yahoo.co.uk> wrote in message
news:c0**********@aspen.sucs.soton.ac.uk...
我是新手。请帮忙。以下警告由gcc-3.2.2
编译器(pc Linux)发出:
========================= ========================= ================
read_raw_data.c:51 :警告:赋值使整数指针
没有演员
=============================== =================== ================

当下面的代码是编译。违规声明
是calloc。 main()函数中的类似语句没有任何
警告。我是否允许在main()或
中使用calloc(和malloc)我做的事情非常愚蠢?
================== ===========================
init(char *文件,double * grid,\
double * start ){

double * depth;
int pts
depth = calloc(pts,sizeof(double));

}
= ===========================================
谢谢。

MK
I am a newbie. Please help. The following warning is issued by gcc-3.2.2
compiler (pc Linux):
================================================== ================
read_raw_data.c:51: warning: assignment makes pointer from integer
without a cast
================================================== ================

when the following piece of code was compiled. The offending statement
is calloc. A similar statement in the main() function passes without any
warning. Am I allowed to use calloc (and malloc) only within main() or
am I doing something very silly ??
=============================================
init(char *file, double *grid, \
double *start){
double *depth;
int pts
depth = calloc(pts,sizeof (double));

}
============================================

Thanks.

MK





" Ricky Cormier" < evilrix @ [@@@] hotmail.com>在消息中写道

新闻:K7 ************** @ newsfep3-gui.server.ntli.net ...

"Ricky Cormier" <evilrix@[@@@]hotmail.com> wrote in message
news:K7**************@newsfep3-gui.server.ntli.net...
MK ,

1.你没有用值初始化pts
2.你应该将calloc返回的(void *)转换为(double *)


不,不,不,不。这隐藏了错误,只是不需要。

3.当声明指针时,最好将它们初始化为NULL以启动(内存函数如free()将忽略NULL 's)
MK,

1. You are not initialising pts with a value
2. You should cast the (void *) returned from calloc to (double *)
No, no no no no. That hides bugs and is just not required.
3. When declaring pointers it''s a good idea to initialise them to NULL to
start with (memory functions like free() will ignore NULL''s)




这是迟钝的。首先,错误时他们将返回NULL。


第二位我的taquitos!


Tom



This is retarded. First off, on error they will return NULL anyways.

Second where are my taquitos!

Tom


这篇关于使用calloc / malloc时有助于警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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