Re: Comments on my code?



On Sep 1, 8:22 pm, Keith Thompson <ks...@xxxxxxx> wrote:
Harald van D k <true...@xxxxxxxxx> writes:

Platonic Solid wrote:
char *r, *malloc(), *strcpy();

On the contrary, I've declared malloc correctly and store its return
value in a char *.

No, you haven't declared malloc correctly.

Specifically, malloc returns void*, not char*, and it takes an
argument of type size_t. Since you didn't declare the argument type,
something as simple as malloc(42) invokes undefined behavior, since
you're passing an argument of type int.

If you had declared it correctly as
void *malloc(size_t);
then a call like malloc(42) would implicitly convert the argument to
size_t -- and a call with a non-numeric argument would trigger an
error message.

Am I right in thinking that first standard promotions would apply, and
only then would the result be converted to size_t?

But the best way to provide the correct declaration is not to
manually re-write it yourself. It's to provide
#include <stdlib.h>
at the top of your source file. There's simply no good reason not to
do it that way.

The more correct information you can provide for the compiler, the
more it can help you. A lot of the facilities for this, particularly
function prototypes, were introduced with the ANSI C standard in 1989.

Prototypes were introduced into C 18 years ago. There are no longer
any significant compilers still being used that don't support them, so
there's no good reason not to use them, along with a number of other
features introduced by ANSI. (The latest ISO C standard, C99, hasn't
caught on as well, so there are still good reasons to avoid
C99-specific features.)

Your code is archaic. You can still write working archaic code if
you're careful enough, but you really should learn the language as it
is today. Reading K&R2 is probably the best way to do this.

--
Keith Thompson (The_Other_Keith) ks...@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


.



Relevant Pages

  • Re: Comments on my code?
    ... value in a char *. ... void *malloc; ... There's simply no good reason not to ... You can still write working archaic code if ...
    (comp.lang.c)
  • Re: Comments on my code?
    ... value in a char *. ... Since you didn't declare the argument type, ... void *malloc; ... Since 42 is already an integer, what standard promotions are you ...
    (comp.lang.c)
  • Re: error: expected ) before * token -- What is this ?
    ... char* helo, char* sender) { ... declare a type named `SPF_request_t', ... typedef struct blist bl; ... void addlog(int num_args, ... ...
    (comp.lang.c)
  • Re: char* program-flow crossroads
    ... > * this out of some reason ... > void do{ ... since the storage to the char pointer ... enum CMD cmd; ...
    (comp.lang.c)
  • Re: Casts
    ... I still can't think of a single *good* reason for casting a void * to ... a char *. ... If p points to a properly aligned int, ...
    (comp.lang.c)