From: Dan.Pop@cern.ch (Dan Pop)
Subject: Re: warning: cast from pointer
Date: 2000/12/06 [thread overview]
Message-ID: <90ll5e$cg4$1@sunnews.cern.ch> (raw)
In-Reply-To: 90l6uf$v8h$1@nnrp1.deja.com
In <90l6uf$v8h$1@nnrp1.deja.com> Dr. Unk <dr_unk@hotmail.com> writes:
>I would like know how to clean up a source, so that I do not get any
>more "warning: cast from pointer to integer of different size" or
>"incompatible pointer" warnings. I know that in most cases they are
>just that, warnings.
They *always* indicate bugs in your code. If your code "works", it is
purely by accident.
>But sometimes, I believe them to be the cause if
>many seg faults and floating point exceptions.
Indeed.
>My C knowledge is basic-level (2 courses taken). My system is an
>AlphaPC 164SX, Glibc-2.1.3, GCC-2.95.2, Linux-2.2.17.
Make sure that you include the required headers for *all* the library
functions you're calling and that you declare *all* your functions before
calling them.
Example of buggy code giving the diagnostic mentioned above on Alpha:
ues5:~/tmp 333> cat test.c
int main()
{
char *p = (char *)malloc(100); /* malloc *requires* <stdlib.h> */
return 0;
}
ues5:~/tmp 334> gcc test.c
test.c: In function `main':
test.c:3: warning: cast to pointer from integer of different size
ues5:~/tmp 335> gcc -Wall test.c
test.c: In function `main':
test.c:3: warning: implicit declaration of function `malloc'
test.c:3: warning: cast to pointer from integer of different size
test.c:3: warning: unused variable `p'
As you can see, -Wall also told you what is the exact problem triggering
the other warning. You *really* should get your code to compile cleanly
with gcc -Wall.
The right way to fix my example is to:
1. Include <stdlib.h>
2. Drop the cast in front of malloc. This cast will give a clean
compile (if you don't use -Wall) on a 32-bit platform, masking the
bug in the code.
Dan
--
Dan Pop
CERN, IT Division
Email: Dan.Pop@cern.ch
Mail: CERN - IT, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland
next prev parent reply other threads:[~2000-12-06 5:00 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2000-12-06 5:00 warning: cast from pointer Dr. Unk
2000-12-06 5:00 ` Dan Pop [this message]
-- strict thread matches above, loose matches on Subject: below --
2000-12-06 5:00 Dr. Unk
2000-12-07 5:00 ` arnoud
read-only archive
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox