lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]




On 27/06/2016 15:12, Roberto Ierusalimschy wrote:
It was thus said that the Great Irfan Adilovic once stated:
In the 5.3.3 release, lua/lstrlib.cc:936:

     char *ppoint = memchr(buff, point, nb);

is not valid c++ code. This should be fixed with a c-style cast:

   But it *is* valid C code.  In C, it's recommended *not* to cast the result
of malloc() as it can hide errors (mainly missing declarations---in C,
without a prototype, the compiler will assume a function returns an int).

We always use -Wmissing-prototypes, so this does not seem to be an
issue. Note that Lua already casts all the result of malloc (see macros
in 'lmem.h')---otherwise there would be lots of errors like this one.


Wouldn't it be useful to use some macro magic to remove the cast when the code is built as plain C, so that the code the compiler sees is conforming both as C and as C++?

For example define a function-like macro like

MALLOC_CAST(typename, expr)

that expands to nothing when compiled as C?

-- Roberto



Cheers!

-- Lorenzo