Re: Returning int pointer ?
s0suk3@gmail.co m wrote:
Old myth? I _continue_ to see newly written code that crashes because it was
developed on a system w/ 32-bit pointers and somebody--the same person or
co-worker--compiles it on a system where pointers are 64-bits, and the
original author forgot to include stdlib.h.
I fail to see how a rule which is more-or-less "let the computer do the work
for you" could possibly be reasonably chastized generally. It's the epitome
of good sense, and consonant with the entire purpose of compilers; indeed,
computers in general!
The same goes for recommending `sizeof object' rather than `sizeof (type)',
although this latter style is less bug prone in practice than casting
malloc().
In my experience, those who cast malloc() are C++ programmers. One of the
defining distinctions between C and C++ is that C++ has stronger typing.
Casting malloc() betrays poor comprehension of the C type system.
s0suk3@gmail.co m wrote:
On Oct 18, 2:41?pm, Andrey Tarasevich <andreytarasevi ch@hotmail.com>
wrote:
wrote:
Mahendra wrote:
Don't cast the results of memory allocation functions.
I have following code snippet -
int *createIncidenc eMatrix(int numEdges, int numVertices) {
? ? int *p = (int *) calloc((numEdge s*numVertices), sizeof(int));
? ? int *p = (int *) calloc((numEdge s*numVertices), sizeof(int));
Why specifically memory allocation functions? That argument would
apply to any function that returns a void *. For example, you wouldn't
want to cast a memcpy() call either, simply because it's not
necessary. However, you're probably just saying it because of
comp.lang.c's old myth that malloc() shouldn't be cast (for whatever
reason).
apply to any function that returns a void *. For example, you wouldn't
want to cast a memcpy() call either, simply because it's not
necessary. However, you're probably just saying it because of
comp.lang.c's old myth that malloc() shouldn't be cast (for whatever
reason).
developed on a system w/ 32-bit pointers and somebody--the same person or
co-worker--compiles it on a system where pointers are 64-bits, and the
original author forgot to include stdlib.h.
I fail to see how a rule which is more-or-less "let the computer do the work
for you" could possibly be reasonably chastized generally. It's the epitome
of good sense, and consonant with the entire purpose of compilers; indeed,
computers in general!
The same goes for recommending `sizeof object' rather than `sizeof (type)',
although this latter style is less bug prone in practice than casting
malloc().
In my experience, those who cast malloc() are C++ programmers. One of the
defining distinctions between C and C++ is that C++ has stronger typing.
Casting malloc() betrays poor comprehension of the C type system.
Comment