[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Qemu-devel] [PATCH] Win32: Don't remove const attribute in type cas
From: |
Stefan Weil |
Subject: |
Re: [Qemu-devel] [PATCH] Win32: Don't remove const attribute in type casts. |
Date: |
Sat, 13 Jun 2009 20:58:58 +0200 |
User-agent: |
Mozilla-Thunderbird 2.0.0.19 (X11/20090103) |
Jamie Lokier schrieb:
> Stefan Weil wrote:
>> Type casts removing the const attribute are bad because
>> they hide the fact that the argument remains const.
>>
>> They also result in a compiler warning (at least with MS-C).
>
>> - return sendto(s->fd, (void *)buf, size, 0,
>> + return sendto(s->fd, (const void *)buf, size, 0,
>> (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst));
>
> 1. Why isn't the (struct sockaddr *) const too?
>
> It is declared like this in at least one Win32 header file:
>
> WINSOCK_API_LINKAGE int PASCAL sendto(SOCKET, const char*,
> int, int,
> const struct sockaddr*, int);
>
> So why does the buffer pointer need to be const, but the sockaddr
> does not, for MS-C to be happy?
>
> 2. Possibly just a historical note. Passing a const pointer will
> produce compiler warnings or even errors on any platform where
> sendto is declared in system headers without const pointer
> arguments. I think some old unixes did that, but probably QEMU
> isn't targetting any of them.
Correct.
>
> However, it might be why the (void *) cast was put there in the
> first place, as there is no need for any cast at all on platforms
> where sendto() is declared the modern way.
>
> 3. See 2 - maybe just get rid of the cast?
>
> -- Jamie
>
const was not added to the type cast because the
function prototype allows a const pointer argument.
const was added because the casted value (buf) has this attribute.
&s->dgram_dst is part of s which does not have a const attribute,
so there is no need to add const to that type cast.
Replacing uint8_t * by void * in many QEMU's functions
might be the best solution to get rid of type casts,
but that can only be done by a maintainer (or at least
the maintainers must support such changes).
Regards,
Stefan