Page 117 - Programming Microcontrollers in C
P. 117

102    Chapter 2  Advanced C Topics

                              The function prototype of the function malloc() is contained
                          in stdlib.h. This function returns a pointer to a type void. If
                          you will recall the rules of arithmetic for pointers, a pointer to a type
                          void can be assigned to any other pointer type, and vice versa. There­
                          fore, it is not necessary to cast the return from the malloc call to
                          the type char* as was done above. In fact, there are people that say
                          that you should not perform this cast. If you make a mistake and
                          do not include the header stdlib.h, then the compiler would as­
                          sume that the return from the malloc function call is an int. The
                          cast operation would introduce a bug into your code if the header
                          were not included. The error of the missing header file would be
                          identified by the compiler if there is no cast because the program
                          would be loathe to assign an integer to a pointer. Therefore, the code
                          without the cast is more robust.
                              I personally object to this logic. The current trend is to insist that
                          all operands in expressions that are not of the correct type be cast
                          to the correct type prior to execution of any operators. This trend
                          largely ignores the automatic type promotion that takes place when
                          there are mixed types within a single expression. In fact, many pro­
                          gramming standards insist that all mixed types be cast explicitly. In
                          such an environment, it seems rather silly that one case be singled
                          out and done differently.
                              The function talloc() also makes use of the malloc()
                          function. In this case, the argument of the malloc() call is the
                          sizeof operator. sizeof is a C operator that returns the size of
                          the argument. sizeof is an operator in C and requires no prototype.
                   /* talloc : make a tnode */
                   TNODE *talloc(void)
                   {
                       return (TNODE *) malloc(sizeof(TNODE));
                   }
                              The memory allocation function malloc() returns a pointer to
                          the basic memory size of the system. It is always necessary to cast
                          the return from malloc() onto the type of variable that the return
                          must point to. In this case, the cast is to the type pointer to TNODE.
                          In strsave(), the cast was to the type pointer to char. There­
                          fore, the statement
   112   113   114   115   116   117   118   119   120   121   122