pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jeroen T. Vermeulen <jtv@xs4all.nl>
To: David Stanaway <david@stanaway.net>
Cc: pgsql-interfaces@postgresql.org
Subject: Re: Problem with PQexecPrepared
Date: Thu, 10 Jun 2004 00:21:14 +0200
Message-ID: <20040609222114.GI91662@xs4all.nl> (raw)
In-Reply-To: <1086817452.10544.5.camel@dmxnocws13.dialmex.net>
References: <1086817452.10544.5.camel@dmxnocws13.dialmex.net>

On Wed, Jun 09, 2004 at 04:44:12PM -0500, David Stanaway wrote:
 
> The type I am passing is a char** and it is expecting const char* const*

I'm not sure I understand your question entirely, so forgive me if I'm
telling you things you already know...
 
The C type system needs to be strict here to guard against the case where
people try pass a "char **" argument to a "const char **" parameter.

Why is that a problem?

Because the function might try to insert a "const char *" into the array,
thinking it got passed an array of "const char *"s.  But after the function
returned, the caller might overwrite the actual string this pointer pointed
to because it never told the function that the chars were to be kept const.
This is a case where "adding a const" is not type-safe.

Not that it would be a problem here, because the array itself is const
and so the function could never write its own pointer into it.  I think
it's one of those rare situations where a cast is justified.


> The parameters I am passing in a generated at runtime so I can supply a
> const pointer.

Generating something at runtime doesn't mean you can't have a const pointer
to it.  Declaring the thing a pointer points to "const" is a promise that
you won't use the pointer to modify that thing.  Which doesn't mean that
it can't be modified through another pointer!


Jeroen




view thread (6+ messages)  latest in thread

Message-ID: <20040609222114.GI91662@xs4all.nl>
Permalink:  ../20040609222114.GI91662@xs4all.nl/
Also on:    postgresql.org/message-id/20040609222114.GI91662@xs4all.nl

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-interfaces@postgresql.org
  Cc: jtv@xs4all.nl, david@stanaway.net
  Subject: Re: Problem with PQexecPrepared
  In-Reply-To: <20040609222114.GI91662@xs4all.nl>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox