Re: design question

From: Shea Martin (smartin_at_arcis.com)
Date: 08/23/04


Date: Mon, 23 Aug 2004 08:24:34 -0600

David Hilsee wrote:
> "Shea Martin" <smartin@arcis.com> wrote in message
> news:2on77vFcka4kU1@uni-berlin.de...
> <snip>
>
>>I realize the danger of shallow copies, I was just hoping that there might
>
> way
>
>>to save a deep copy, when I don't need it (without overloading).
>
>
> Are you concerned about performance? For a string class, I would bet that
> the overhead of dynamically allocating the array has more of an adverse
> impact on performance than the actual copy. You could run some tests to
> determine what is the problem. If there is a performance problem with the
> dynamic allocation, have you considered using the old trick where a
> fixed-width buffer is used for smaller strings and a dynamically-allocated
> one is used for larger strings? It might result in cleaner code and more
> efficient behavior for the (usually common) smaller strings. Some
> std::string implementations (Dinkumware's?) do this.
>
> If you wanted to avoid a deep copy, you could pass an extra "bool
> doDeepCopy" to the constructor and cross your fingers that you always
> specify the right value, but that could get tricky.
>
I use a plus or minus system: when allocating I always allocate the amount I
need plus a little more. I also don't bother to shrink my buffer unless the
length of the string varies from the buffer size by a certain threshold.

I realize that the allocate is much slower than the copy. That is why a shallow
copy in certain situations is would avoid an array allocation all together.

Actually what I do now, is have a private method called setBuffer. When a const
char is passed, and I know I won't be altering its contents, I assign the const
char array to the char array member of the string class, and call the same
method using the newly created string. While this approach still requires
overloading, at least it lets me put all my logic in one method instead of doing
copy paste. I still don't really like this method, as it makes me feel dirty.

~S



Relevant Pages

  • Re: Delimited String to Array
    ... The casting is not required, and neither is the parsing of the string ... to make explicit what the compiler does implicitly: ... new char array, then loading it with the first character of a string. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Fast and Safe C Strings: User friendly C macros to Declare and use C Strings.
    ... simply a char array - and passed it to a function that expects a string. ... One could also say that an unsheathed sword is not dangerous. ... no danger attached to a sword does not make it safer. ...
    (comp.lang.c)
  • Re: malloc vs new for POD types
    ... I'm allocating the memory by ... > I'm calling the constructor by myself, and I'm calling the destructor by ... special string, and then working with the string. ... > Ofcourse, in the above, the dynamic memory allocation is unnecessary. ...
    (comp.lang.cpp)
  • Re: pcl: is LIST the same as ?
    ... >'casting away const' (although most compilers seem to be in denial;) ... >Then, to modify the string, you're supposed to be very explicit about ... that the first creates a pointer to an unnamed char array (the string ... many compilers will, in the first case, relocate the string constant ...
    (comp.lang.lisp)
  • Re: new String ( byte[] , encoding ) under the hood
    ... >characters to make an immutable char array, the place to achieve savings ... I wonder if there could be some way to hand off a char array to be ... inserted in a string. ...
    (comp.lang.java.programmer)