2 style questions

From: Sander (i_at_bleat.nospam.com)
Date: 12/21/03


Date: Sun, 21 Dec 2003 08:48:45 +0100

1.
I was having a discussion with somebody and I think it's just religious.
We are developing some software and he was looking through my code.
I use if (pointer) to test a pointer for NULL. He says it must be if
(p!=NULL).
The arguments are that it's more readable and that if (p) is not portable.
I said the first is a style issue and that I think if(p) is more readable
and that the second is plain wrong.

So, is he right about the portability issue? is if (p) not strictly correct
C code?

2.
Then I started looking through his code and what I noticed that he very
often does things like this:

char *func(char **out)
{
  *out = malloc(100);
  return malloc(10);
}

This might also be a style issue, but it's a style where it's very easy to
introduce bugs (and indeed I found a few)
and even more easy to introduce bugs if you don't know the internals of the
function. So, what do you think about this style?



Relevant Pages

  • Re: 2 style questions
    ... Sander wrote: ... > I use if (pointer) to test a pointer for NULL. ... is he right about the portability issue? ... > and even more easy to introduce bugs if you don't know the internals of the ...
    (comp.lang.c)
  • Re: 2 style questions
    ... > I use if (pointer) to test a pointer for NULL. ... is he right about the portability issue? ... programming oriented editors let you jump right to the declaration. ... > and even more easy to introduce bugs if you don't know the internals of the ...
    (comp.lang.c)
  • Re: When to check the return value of malloc
    ... This is true if one assumes a constant number of bugs per line of code. ... A direct pointer need never have been used. ... asserts caught everything. ... It is much more difficult, for instance, to verify that a ...
    (comp.lang.c)
  • Re: When to check the return value of malloc
    ... something creates a lot of bugs too. ... If I pass a pointer to it, what happens if the pointer is a ... You are now requiring the CALLING CODE to do the allocation and release. ... it would initialize the printing system. ...
    (comp.lang.c)
  • Re: When to check the return value of malloc
    ... If it is creating bugs then, it is ineffective and the practice needs to be ... You can't detect an invalid pointer, ... handling code no longer does its own allocation. ... The printing system is used by the rest of the program; ...
    (comp.lang.c)