Ok here is a typical function with memory and pointers, gettbl is used to
build a Look Up Table used later...
double *mkary(size_t siz)
{
int bytes=(size_t)siz*sizeof(double); // nb double is size 4
Why cast 'siz' to a type it already has? Moreover, when you
emphasize the "size_t-ness" that much, why isn't 'bytes'
then also a 'size_t'? And that the size of double is 4 is
specific to your platform, on others it may have a comple-
tely different value, so your comment is confusing at best.
double *ary=(double *)malloc(bytes+1);
Why the cast of the return value of malloc() (or did you forget
to include <stdlib.h> and got a compiler warning)? And why adding
1 to 'bytes' - this is an array of doubles and not a string where
may you have to add 1 for the trailing '\0' character when you
calculate the length with strlen()? I would write that as
double *ary = malloc( siz * sizeof *ary );
and drop the line that calculates 'bytes'. (Of course, you
should always test if malloc() succeeded, if it didn't all
bets are off since then 'ary' will be just a NULL pointer).
for(bytes=0;bytes<siz;++bytes)
*(ary+bytes)=gettbl(bytes);
Mmm, why use '*(ary+bytes)' when 'ary[bytes]' would do equally
well? I know they are exactly identical, but I would consider
the second form to be easier to read. My personal feeling is
also that using 'bytes' both as a variable for the number of
bytes you want to malloc() and later as an index into an array
can be confusing, I would prefer to use two distinct variables
and let the compiler worry about optimizing that.
Why cast 'ary' to a type it already has? I would strongly
recommend to avoid using casts unless they're really needed.
I consider a cast as a red flag that something unusual is
going on that may need closer scrutiny - if everything works
according to the normal typing rules a cast wouldn't be needed.
}
double *freeary(double *ary)
{
void *vp=(void*)ary;
if(vp!=(void*)NULL) free(vp);
return (double*)vp;
}
Not a single cast in this function is needed. They really only
make reading the code much harder. And you don't need to check
if 'ary' (or 'vp') is NULL before calling free() on it - calling
free() on a NULL pointer is just a NOP. In case that 'ary' isn't
a pointer you got from malloc(), realloc() or calloc() you're
screwed anyway and comparing it to NULL won't help in the least.
But this function invokes a very sneaky form of undefined be-
haviour. free() doesn't set the pointer you call it on to NULL
(I guess that's what you assume it would do, otherwise why re-
turn it's value?). But using a pointer after calling free() on
it actually does invoke undefined behaviour (that's right, using
the value of a pointer after free() does that, not just derefe-
rencing it). What I guess you intended to do in that function is
probably just
double * freeary( double * ary )
{
free( ary );
return NULL;
}
But it's rather unlikely that any of this is the actual culprit
(except maybe returning the now useless value of 'ary' if you
later make decisions based on its values).
Regards, Jens