[LC++]Curiosity

Dr Mark H Phillips mark at austrics.com.au
Wed Dec 3 15:29:01 UTC 2003


Oh, I forgot to include the output I get:

$ a.out
 
lemon = "B"
 
ned = B
 
lemon = "B"B??"B"?????8?X?x??????????8?X?x??????$


And on another machine, the last line above was simply "lemon = $", ie
nothing more was printed until the $ prompt.

Cheers,

Mark.

On Wed, 2003-12-03 at 17:40, Dr Mark H Phillips wrote:
> Hi,
> 
> I have found what appears to be either a compiler
> error or an error in the standard string library.  It
> is rather odd.
> 
> I've attached the code below.  Basically I start with
> a pointer to a (string,int) pair.  Then I call a
> "function" which shouldn't harm anything, but afterwards
> when I try to print the pair, the string has been
> corrupted.
> 
> Now the version of g++ I was using was 2.95.4.  I did
> try compiling it using version 3.2.2 and the problem
> disappeared.  I have also found that by changing
> Dequote to:
> 
> struct Dequote {
>   std::string operator()(std::string const& token) const {
> 
>   etc...
> 
> (so that there is now a const reference in there) everything
> works fine under 2.95.4.
> 
> So I've found a work-around.  The reason I'm posting this to
> the list is that I'm curious.  I'm wondering whether this is
> a compiler error that they fixed in the 3.*.* series, or whether
> the earlier string implementation was buggy, or whether something
> else is going on.  Does anyone know?
> 
> Cheers,
> 
> Mark.
> 
> 
> ______________________________________________________________________
> #include <iostream>
> #include <string>
> #include <utility>
> 
> struct Dequote {
>   std::string operator()(std::string token) const {
>     std::string ret = "";
>     ret+= token[1];
>     return ret;
>   }
> } dequote;
> 
> int main() {
> 
>   std::pair<std::string, int>* 
>       lemon(new std::pair<std::string, int>("\"B\"", 99));
> 
>   std::cerr<<"\nlemon = "<<lemon->first<<"\n";
> 
>   std::string ned = 
>       dequote(((std::pair<std::string, int> const)(*lemon)).first);
> 
>   std::cerr<<"\nned = "<<ned<<"\n";
>  
>   std::cerr<<"\nlemon = "<<lemon->first<<".\n";
> 
>   std::cerr<<"\nthe end.\n";
> 
> }




More information about the tuxCPProgramming mailing list