[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-krn-devel
Subject:    Re: Two bugs in mime++ (fwd)
From:       Roberto Alsina <ralsina () unl ! edu ! ar>
Date:       1997-08-04 10:05:56
[Download RAW message or body]

Here are some extra comments by Doug Sauder about the usage of DwString


 ("\''/").__..-''"`-. .         Roberto Alsina
 `9_ 9  )   `-. (    ).`-._.`)  ralsina@unl.edu.ar
 (_Y_.)' ._   ) `._`.  " -.-'   Centro de Telematica
  _..`-'_..-_/ /-'_.'           Universidad Nacional del Litoral
(l)-'' ((i).' ((!.'             Santa Fe - Argentina


---------- Forwarded message ----------
Date: Sat, 2 Aug 1997 22:42:12 +0000
From: Doug Sauder <dwsauder@fwb.gulf.net>
To: Roberto Alsina <ralsina@unl.edu.ar>
Subject: Re: Two bugs in mime++

> It does, at least for my problem. I haven't looked into Magnus's code yet.
> I have a small question, though: Am I supposed to delete the result of 
> c_str()? I guess not because I get segfault if I do :-)
> But I was just curious about why, since it's a copy.

It's very simple.  DwString uses copy-on-write.  So whenever a
substring is taken of another string, the two strings share the same
string rep.  c_str() may have to modify the internal string rep, in
order to write the terminating NUL, so that's why it does a copy.
c_str() returns a pointer to the internal buffer, so it definitely
shouldn't be deleted.

Of course, you could do the copy yourself, like this

   size_t len = str.length();
   char* buf = new char[len+1];
   strncpy(buf, str.data(), len);
   buf[len] = 0;

If you need a copy, then the above is better than the following:

   char* buf = strdup(str.c_str());

since this will probably cause the string to be copied twice.

-- DWS

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic