On 09/04/2007 09:47 AM, Andy Ross wrote:
> .... the patch ....

I assume we are still talking about:
http://sourceforge.net/tracker/index.php?func=detail&aid=1584727&group_id=382&atid=100382

>  ....  strchr()
> returns a pointer into the *same* memory it got.  The constness needs
> to be synchronized between the pointers, which is outside the
> capabilities of the C language. 

It is not outside the capabilities of the c++ language ... which is
the language of the patch.  The capability is demonstrated in the
example below.

The whole line of argument is pointless anyway, because the constness
is a property of the pointer, not a property of the memory.

> So programmers have to choose between
> a slightly "unsafe" function that drops const and one that requires a
> cast to use with a non-const string.

No, in general they don't need to choose, as explained below.

================================

Returning to the higher-level discussion, it is not necessary to do a
strdup in this situation, as the following constructive suggestion
illustrates.

               using namespace std;
               #include <iostream>

               const char* strchr(const char* foo){
                 cout << "const" << endl;
                 return foo+3;
               }

               char* strchr(char* foo){
                 cout << "not const, implemented in terms of: " ;
                 const char* bar(foo);
                 return foo + (strchr(bar) - foo);
               }

               int main(){
                 char aa[100];
                 const char* xx(aa);
                 const char* yy(aa);
                 yy = strchr(xx);      // const
                 yy = strchr(aa);      // non const
               }
The output of this program is:
          const
          not const, implemented in terms of: const


Notes:

1) The trick of foo+(bar-foo) is a standard way of transferring information
from a const pointer (bar) to a non-const pointer (foo).

2) I chose to embody this trick in a wrapper function, partly to make
it easier to insert the explanatory messages.  It would be well within
the reasonable envelope to put the foo+(bar-foo) trick inline, with no
wrapper.


==================================================================================

> Oh, good grief:
> 
>   $ echo 'void foo(const char* p){*(char*)p=0;}' > tmp.c
>   $ gcc -S -c -o - tmp.c

Good grief yourself.  Do you really think I don't know that ...
especially given that I explicitly mentioned it a few lines farther
down?

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >>  http://get.splunk.com/
_______________________________________________
Flightgear-devel mailing list
Flightgear-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/flightgear-devel

Reply via email to