Re: is this code valid?
On Jun 4, 1:36 am, "jason.cipri...@gmail.com"
<jason.cipri...@gmail.com> wrote:
> On Jun 3, 4:18 pm, "Chris Thomasson" <cris...@comcast.net> wrote:
> > "Victor Bazarov" <v.Abaza...@comAcast.net> wrote in message
> > > Chris Thomasson wrote:
> > >> : m_id(id), m_name("Default Producer"), m_Buffer(_Buffer) {
> > >> std::printf("(%p)->Producer::Producer()\n", (void*)this);
> > > I don't believe you need to cast 'this' here.
> > Well, I thought that you could only pass a void* pointer to
> > represent a %p formatter.
> You can pass any data pointer, it's all the same.
That is simply false. Technically, you can only pass a void*; I
don't see anything in ISO/IEC 9899 which would even allow void
const*. In practice, unless the implementation explicitly
verifies, you'll also be able to pass char* and char const*,
since they are required to have the same size and representation
as a void*. Anything else is undefined behavior, and I've
worked on machines where it would cause random junk to be
output (and could even cause a core dump if there were also a %s
or a %f elsewhere in the format string).
> However, GCC's handy printf() warnings also warn when you pass
> a non-void* to a %p, so casting it does get rid of that
> warning. Other than squashing a compiler warning you don't
> have to do the cast.
According to the C standard, you do.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
|