Am 24.09.22 um 09:38 schrieb Arrigo Marchiori:
Hello,

On Fri, Sep 23, 2022 at 07:08:26AM +0200, Peter Kovacs wrote:

Hi all,

We cast int to (void*) -> (example in main/sw/source/ui/fldui/changedb.cxx
line 234)

In general this sounds like not a sane idea to me, see discussions [1] [2]
[3]. The solution that the c++ community found is the use of intptr_t, which
is defined in C99.

In general I would like to get rid of these casts. However as a quick fix it
would make sense to use intptr_t as a first measure.
IMHO casting int to void* is not as bad as it seems.

I just note, I dont like this pattern in general. It makes maintenance in general more difficult.

Your cited sources explains that there would be problems with
_round-tripping_ i.e. using an int type to pass a pointer.

The reason is that on some architectures pointers (void*) are
represented with more bits than integers. Notable example: Linux on
x86_64.

As long as we are using pointers as ``general purpose integers'' we
should have no problems because they are ``big enough'' to contain any
integer value _and_ any pointer.

From my understanding the warning we receive is triggered when:

|sizeof(void*)!=sizeof(int)|

|Which means the issue can aizue on machines / compilers.|

|I dont think ignoriing this issue is a good measurement. Especially, since the CPU Architectures change now.
|


Is there any reason that would block the move to C99? I guess C99 should be
supported by all the compilers we require . (c11 is currently not available
on all Plattforms we support.)
Unfortunately, Visual Studio 2008 does not support C99.  Specifically
for this case, it is missing the definition of the intptr_t type.

Wikipedia [4] gives more information about C99 support in Visual Studio.

I hope this helps.

Ahhh thanks. So C99 is not a way at this point. I guess casting to long instead is also not great option.

I think of something. It has not the highest priority. Buit as expected a solution does not seem to be simple.


[1]https://stackoverflow.com/questions/26805461/why-do-i-get-cast-from-pointer-to-integer-of-different-size-error

[2]https://stackoverflow.com/questions/25381610/cast-int-to-pointer-why-cast-to-long-first-as-in-p-void-42

[3]https://stackoverflow.com/questions/6326338/why-when-to-use-intptr-t-for-type-casting-in-c
[4]https://en.wikipedia.org/wiki/Microsoft_Visual_C%2B%2B#C

Reply via email to