[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH] Don't cast the return of malloc() and friends
From: |
Benno Schulenberg |
Subject: |
Re: [PATCH] Don't cast the return of malloc() and friends |
Date: |
Wed, 2 Sep 2020 09:44:08 +0200 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 |
Op 01-09-2020 om 19:41 schreef Hussam al-Homsi:
>>> Subject: [PATCH] Don't cast the return of malloc() and friends Because
>>> doing so is superfluous.
>>
>> It may be redundant, but does it do any harm?
>
> Other than wasting some space, it can do harm if one forgets to include
> <stdlib.h> and older C compilers then suppose a return of int and trigger the
> wrong conversion from int to a pointer type.
This is not relevant, as I use fairly recent compilers that would warn about
this, and I make sure nano compiles without warnings.
>> Also, if ever the type of one of the relevant variables is changed, the cast
>> will cause compilation errors -- without the cast the compilation would
>> silently but probably wrongly succeed.
>
> The conversion from void*, the return type of malloc(), to the target type is
> automatic and doesn't need any intervention.
Of course. But not relevant to the above argument.
> Now about changes to target types. That would only be a problem if the
> argument of malloc() references the target type: T* p = malloc(sizeof (T));
> For that problem I suggest using this idiom instead: T* p = malloc(sizeof
> *p); Where the type info is specified only once. But that's for another time.
> :)
Such a patch would not be accepted, as I want to see what type the malloc()
is for.
Benno
signature.asc
Description: OpenPGP digital signature