prev up next   top/contents search

comp.lang.c FAQ list · Question 7.15

Q: malloc is returning crazy pointer values, but I did read question 7.6 and I have included the line

	extern void *malloc();
before I call it.

A: malloc accepts an argument of type size_t, and size_t may be defined as unsigned long. If you are passing ints (or even unsigned ints), malloc may be receiving garbage (or similarly if you are passing a long but size_t is int).

In general, it is much, much safer to declare standard library functions by #including the appropriate header files, rather than typing extern declarations yourself. See also question 7.16.

(A related problem is that it is not safe to print size_t values, including the result of sizeof, using printf's %d format. The portable approach is to use an explicit (unsigned long) cast, and %lu format: printf("%lu\n", (unsigned long)sizeof(int)). See also question 15.3.)

References: ISO Sec. 7.1.6, Sec. 7.1.7

prev up next   contents search
about this FAQ list   about eskimo   search   feedback   copyright

Hosted by Eskimo North