Re: [HACKERS] Add some const decorations to prototypes - Mailing list pgsql-hackers

From Fabien COELHO
Subject Re: [HACKERS] Add some const decorations to prototypes
Date
Msg-id alpine.DEB.2.20.1711101733510.2232@lancre
Whole thread Raw
In response to Re: [HACKERS] Add some const decorations to prototypes  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: [HACKERS] Add some const decorations to prototypes  (Tom Lane <tgl@sss.pgh.pa.us>)
Re: [HACKERS] Add some const decorations to prototypes  (Peter Eisentraut <peter.eisentraut@2ndquadrant.com>)
List pgsql-hackers
Hello Tom,

>>>> ISTM That there is still at least one strange cast:
>>>>> +static const char **LWLockTrancheArray = NULL;
>>>>> +               LWLockTrancheArray = (const char **) // twice
>
>>> These are not cases of "cheating".  This is just the return value of a
>>> memory allocation function being cast from void * to the appropriate
>>> result type.  That is an orthogonal style decision that I have
>>> maintained in these cases.
>
>> Would it make sense that the function returns "const void *", i.e. the
>> cast is not on the const part but on the pointer type part?
>
> Certainly not -- if malloc-like functions returned "const void *" then
> you could never write on allocated space without having casted away
> const.

Ok. Sure.

>        LWLockTrancheArray = (char **)
>            MemoryContextAllocZero(TopMemoryContext,
>                                   LWLockTranchesAllocated * sizeof(char *));
>
> and the reader can see by inspection that the calculation of how much
> to allocate (so many char-* items) is consistent with the char-**
> result.  It is not necessary to go find the declaration of
> LWLockTrancheArray and verify that it's char **, because we trust the
> compiler to whine if char ** isn't assignment-compatible with that.
> But if we left off the cast and just assigned the function result directly
> to the variable, then there would be nothing directly tying the variable's
> type to this allocation computation.

Thanks for the reflesher course about the intricacies of "const".

After your explanation, and on third thoughts, ISTM that the assignment 
should not include "const" in the explicit cast, i.e., use
  extern void * msg_func(void);  const char * msg = (char *) msg_func();

The variable or field is constant, not what the function returns, so
  const char * msg = (const char *) msg_func();

does not really make full sense to me, and moreover the compiler does not 
complain without the const.

-- 
Fabien.


-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

pgsql-hackers by date:

Previous
From: Robert Haas
Date:
Subject: Re: [HACKERS] Incorrect comment for build_child_join_rel
Next
From: Tom Lane
Date:
Subject: Re: [HACKERS] Add some const decorations to prototypes