From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f5392,53889543ba1b8d53 X-Google-Attributes: gidf5392,public X-Google-Thread: 1014db,53889543ba1b8d53 X-Google-Attributes: gid1014db,public X-Google-Thread: 111c93,53889543ba1b8d53 X-Google-Attributes: gid111c93,public From: Dan.Pop@cern.ch (Dan Pop) Subject: Re: warning: cast from pointer Date: 2000/12/06 Message-ID: <90ll5e$cg4$1@sunnews.cern.ch>#1/1 X-Deja-AN: 701792882 References: <90l6uf$v8h$1@nnrp1.deja.com> X-Complaints-To: news@sunnews.cern.ch X-Trace: sunnews.cern.ch 976115694 12804 (None) 137.138.32.79 Organization: CERN European Lab for Particle Physics Newsgroups: comp.lang.c,comp.os.linux.development.system,comp.os.linux.alpha In <90l6uf$v8h$1@nnrp1.deja.com> Dr. Unk 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* */ 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 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