Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 21 Feb 1999 16:37:20 +1100
From:      Bruce Evans <bde@zeta.org.au>
To:        freebsd-current@FreeBSD.ORG, hibma@skylink.it
Subject:   Re: const char *
Message-ID:  <199902210537.QAA13128@godzilla.zeta.org.au>

next in thread | raw e-mail | index | archive | help
>../../dev/usb/ukbd.c: In function `ukbd_detach':
>../../dev/usb/ukbd.c:373: warning: cast discards `const' from pointer
>target type
>...
>It is a consequence of the following type definition:
>
>(sys/bus_private.h)
>struct device {
>...
>    const char*         desc;   /* driver specific description */
>...
>}
>
>One problem I have is that a string like that might be produced by
>concatenating a few strings together.  In the case of USB the desc is
>the string returned by the device. At attach the string is created and
>malloc()-ed, at detach free()-ed. 

At the <sys/bus*.h> (bus) level, `desc' is just a string.  It is const
mainly so that callers can pass a literal string to device_set_desc()
without getting a warning from -Wcast-qual.  Unfortunately, this prevents
the bus level from helping you keep track of how the string was created.

The warning could be avoided using a table of the original (non-const)
addresses of the (malloc()ed) strings passed to device_set_desc().
This is a bit much to avoid a harmless warning, but you would need to
do it if not all strings were malloc()ed.

The warning can be avoided by (ab)using a variant union.  I plan to use
a suitably macroised version of this to fix warnings for library functions.

Bruce


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-current" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199902210537.QAA13128>