On Sat, May 06, 2006 at 12:07:54AM +0200, Edwin Leuven wrote:
> Angus Leeming wrote:
> >LOL! I think You are. You've confused this with "const &"
> 
> and to add to the confusion (at least mine):
> 
> why don't i see any invalid behavior or crashes with my original (wrong) 
> patch?

char const * using_this_returned_value_is_undefined_behaviour()
{
  char const * cdata = std::string("data").c_str();
  return cdata;
}

I think I understand this. The problem is that cdata is made to point to
the start of the string "data" in memory, which is a constant that may
be allocated in two different ways:

1) like a local variable inside the using_...vior() routine, i.e., part
of the instantiation's stack frame;

2) as part of the _code_ of this routine.

In the first case, the stack frame disappears on return, and using
whatever cdata used to point to may or may not bite you. In the second,
the string "data" is a permanent part of the executable binary.

const-ing *cdata may for some compilers influence this behaviour, who
knows.

Does this make sense?

- Martin

Attachment: pgp5ZznT17YQU.pgp
Description: PGP signature

Reply via email to