parjit said:
It is not a homework, it is code the teacher gave us to use as a
component in our homeworks to take care of error checking and unsafeness
in standard functions. We do not have to explain this code but I want to
understand it for my own interest. It is very complicated code, I don't
think I will ever be able to write code this clever
Perhaps the teacher will explain it later. Perhaps participants of a
class will discuss it. Perhaps the function's body will evolve over the
course of instruction. Do you have the opportunity to ask the teacher?
Here are the safe versions of malloc and free. Also confusing to me!
void *safemalloc(int sz)
{
void *r;
return sz<=0 ? 0 :
(r=malloc(sz)) ? r :
((!!printf("fatal error cannot alocate %d bytes dumping core",sz)
&& (exit(1),0)),NULL);
}
Is this a copy and a paste? "alocate" might be a typographical error
for "allocate". If it's not a copy and a paste, did you re-type the
whole thing by looking at a paper? I'm curious about the original
spacing and indenting format, if you please.
One or more generous authors have kindly offered an online indenter,
available at
http://indentcode.net/ That doesn't help very much for a
chain of ternary operators, but might come in handy for you with other
code. There's also 'indent', which might or might not be a command
available in your environment.
I ask about this and mention these formatting tools because if one is
trying to understand code, there might be a visual (or spatial)
challenge which can be alleviated... Maybe not entirely for these
functions.
void safefree(void *p)
{
free(p?p:
(!!printf("fatal error cannot free null pointer dumping core")
&& (exit(1),0),NULL));
}
There appears to be an emphasis on avoiding multiple statements in these
functions. Instead of ';' sequence points, we have ternary conditional
operator ('?:') sequence points and comma operator (',') sequence
points. By avoiding 'if' and restricting statements to declarations and
'return', it might be understood as a means to transition from
mathematics into C, since the rest are operators and function calls
(which are like N-ary operators).
There also appears to be an emphasis on avoiding explicitly identified
local variables wherever possible.
Consider (if you'd like to) an alternative to 'safemalloc':
void *safemalloc(int sz)
{
/* Things we might use. */
int sz_lteq_zero;
void *malloc_ptr; /* Was 'r' */
void *malloc_returns_non_null_ptr;
const char *msg;
/* Things we need #1. */
sz_lteq_zero = sz <= 0;
/* Things we do with things from #1. */
if (sz_lteq_zero)
return 0;
/* Things we need #2. */
malloc_ptr = malloc(sz);
malloc_returns_non_null_ptr = malloc_ptr;
/* Things we do with things from #2. */
if (malloc_returns_non_null_ptr)
return malloc_ptr;
/* Things we need #3. */
msg = "fatal error cannot alocate %d bytes dumping core";
/* Things we do with things from #3. */
return ((!!printf(msg, sz) && (exit(1), 0)), NULL);
}
That example does not attempt to improve the teacher's instructional
'safemalloc' whatsoever, but it attempts to demonstrate how the function
might look using local variables and 'if' statements.
Fun.