Topic: operator const char*


Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1995/07/06
Raw View
In article dea@darkstar.UCSC.EDU, panzer@cse.ucsc.edu (John Panzer) writes:
>In article <3tf7e6$l6j@eurybia.rz.uni-konstanz.de>,
>Dietmar Kuehl <dietmar.kuehl@uni-konstanz.de> wrote:
>>Uwe Tantz (c4037@rphc1.physik.uni-regensburg.de) wrote:
>>: CString SayHello (void) { return "Hello"; }
>>: void TextOut (const char* str) { cout << str; }
>>:
>>: int main(void)
>>: {
>>:   TextOut (SayHello());
>>:   return 0;
>>: }
>>:
>>: I think when TextOut gets its char* (after call of operator const char*),
>>: the temp. CString-object is no longer "in use" and the compiler may
>>: delete it, causing str to point into desert.
>>:
>>: Am I wrong? Is there a proper implementation (without use of global memory)?
>>
>>Yes. class.temporary (12.2) verse 3: "... Temporary objects are destroyed as
>>the last step in evaluating the full-expression that (lexically) contains the
>>point where they were created. ..."
>
>I was under the impression that the new standard string class contains
>no const char* conversion because of this type of problem.  My memory
>says that it contains a member "c_str()" instead -- is this correct?

That's correct. The standard string class has no automatic conversion
to char* precisely to help avoid this kind of problem. You have to ask
specifically for a conversion to char*, in which case you must
take into account the lifetime of the string.

Automatic type conversions tend to be somewhat dangerous, since it may not
be evident from the source code that a conversion is taking place. In the
example above, you might have thought function TextOut accepted a CString
argument, which would be safe, when in fact it requires a char*. Without
an automatic conversion, the compiler will complain about the call, and
you can fix the code.

---
Steve Clamage, stephen.clamage@eng.sun.com







Author: panzer@cse.ucsc.edu (John Panzer)
Date: 1995/07/06
Raw View
In article <3tf7e6$l6j@eurybia.rz.uni-konstanz.de>,
Dietmar Kuehl <dietmar.kuehl@uni-konstanz.de> wrote:
>Uwe Tantz (c4037@rphc1.physik.uni-regensburg.de) wrote:
>: CString SayHello (void) { return "Hello"; }
>: void TextOut (const char* str) { cout << str; }
>:
>: int main(void)
>: {
>:   TextOut (SayHello());
>:   return 0;
>: }
>:
>: I think when TextOut gets its char* (after call of operator const char*),
>: the temp. CString-object is no longer "in use" and the compiler may
>: delete it, causing str to point into desert.
>:
>: Am I wrong? Is there a proper implementation (without use of global memory)?
>
>Yes. class.temporary (12.2) verse 3: "... Temporary objects are destroyed as
>the last step in evaluating the full-expression that (lexically) contains the
>point where they were created. ..."

I was under the impression that the new standard string class contains
no const char* conversion because of this type of problem.  My memory
says that it contains a member "c_str()" instead -- is this correct?

John Panzer






Author: Uwe Tantz <c4037@rphc1.physik.uni-regensburg.de>
Date: 1995/07/05
Raw View
Having read the "Can this delete a linked list?" thread, I ask myself if
(all) string-classes (I know) have the same problem.

For example class CString of MFC (which is bad, I think) works like this:

class CString
{
  public:
  // some ctors, dtor, functions...

  operator const char* (void) const { return m_pchStr; }

  private:
  char* m_pchStr;
};

Now imagine this code:

CString SayHello (void) { return "Hello"; }

void TextOut (const char* str) { cout << str; }

int main(void)
{
  TextOut (SayHello());
  return 0;
}

I think when TextOut gets its char* (after call of operator const char*),
the temp. CString-object is no longer "in use" and the compiler may
delete it, causing str to point into desert.

Am I wrong? Is there a proper implementation (without use of global memory)?

Thanks, Uwe

+---------------------+--------------------------------------------------+
| Uwe-Krischna Tantz  | Tel/Fax: +49 941 / 56 31 40                      |
| Hackengaesschen 4   | EMail: uwe.tantz@physik.uni-regensburg.de        |
| 93047 Regensburg    |                                                  |
+---------------------+--------------------------------------------------+






Author: kuehl@uzwil (Dietmar Kuehl)
Date: 1995/07/05
Raw View
Uwe Tantz (c4037@rphc1.physik.uni-regensburg.de) wrote:
: CString SayHello (void) { return "Hello"; }
: void TextOut (const char* str) { cout << str; }
:
: int main(void)
: {
:   TextOut (SayHello());
:   return 0;
: }
:
: I think when TextOut gets its char* (after call of operator const char*),
: the temp. CString-object is no longer "in use" and the compiler may
: delete it, causing str to point into desert.
:
: Am I wrong? Is there a proper implementation (without use of global memory)?

Yes. class.temporary (12.2) verse 3: "... Temporary objects are destroyed as
the last step in evaluating the full-expression that (lexically) contains the
point where they were created. ..."

dk
--
http://www.informatik.uni-konstanz.de/~kuehl
dietmar.kuehl@uni-konstanz.de
I am a realistic optimist - that's why I appear to be slightly pessimistic