Re: [HACKERS] Add some const decorations to prototypes

Поиск
Список
Период
Сортировка
От Fabien COELHO
Тема Re: [HACKERS] Add some const decorations to prototypes
Дата
Msg-id alpine.DEB.2.20.1711101733510.2232@lancre
обсуждение исходный текст
Ответ на Re: [HACKERS] Add some const decorations to prototypes  (Tom Lane <tgl@sss.pgh.pa.us>)
Ответы Re: [HACKERS] Add some const decorations to prototypes
Re: [HACKERS] Add some const decorations to prototypes
Список 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 по дате отправления:

Предыдущее
От: Robert Haas
Дата:
Сообщение: Re: [HACKERS] Incorrect comment for build_child_join_rel
Следующее
От: Tom Lane
Дата:
Сообщение: Re: [HACKERS] Add some const decorations to prototypes