Re: Why crash ?



"Ulrich Eckhardt" <eckhardt@xxxxxxxxxxxxxx> wrote in message
news:tqgko3-qp1.ln1@xxxxxxxxxxxxxxxxxxxxxx
Eberhard Schefold wrote:
Arnaud Debaene schrieb:
That's a side effect of the "small string optimization" :
std::basic_string has an internal 16 bytes buffer that is used to
hold the string content when it is short enough : this avoid
dynamic memory allocation for small strings.

Yes, but naively, I would assume that the optimal buffer size
depended on a certain number of characters, and therefore would
double with wstring. I might well be wrong, though.

The effort to copy said buffer (which contains PODs and can thus be
copied with memcpy) is the same, regardless of what it holds. The
effort to allocate memory dynamically is also the same, no matter
what it will finally contain. Both only depend on the size of the
buffer, so that's a natural approach.


If you are arguing that a buffer size based on bytes rather than characters
is natural, them I am not convinced.

My understanding is that there is a tradeoff between speed and memory usage.
Allocating a large default buffer increases speed if it means that you don't
need to make an additional allocation, but costs extra memory if that buffer
ends up not being fully utilized.

If the foregoing is correct, then it is not obvious that the loss of speed
in going from 15 to 7 characters in the wide character case is justified by
the greater memory saving in that case. That presumably depends on how one
values speed vs memory and on the size distribution of your strings. To take
an extreme case, suppose that all your strings are 15 characters long. Then
if you care at all about speed, you are plainly not going to want a 7
character limit.

--
John Carson



.



Relevant Pages

  • Re: Alternative to C for system programming
    ... be blocks of memory that are GC and nonGC, ... to call it a systems programming language with GC. ... Just make sure your disk buffer or whatever is ... allocation of memory: I/O module thinks it will need more memory ...
    (comp.programming)
  • Re: callocs, call by ref and function returning in C
    ... > memory allocation and freeing). ... clobbering buffer, so you can never free that memory again. ... you exit without freeing up resources. ...
    (comp.programming)
  • Re: Attention Windows Users
    ... > how there could be a buffer overflow. ... > memory. ... A "dynamic allocation" is one that occurs at run-time. ... a buffer overflow can occur with either a dynamic or static ...
    (rec.aviation.piloting)
  • Re: return a string
    ... Declare a static array of char in your ... mallocmemory for the string inside your function ... >> provide a way to use an alternative allocation function. ... Pass an already allocated buffer and its size to your function. ...
    (comp.lang.c)
  • Re: Memory leak with socket BeginReceive?
    ... First, all allocation ... compact memory below the highest pinned object. ... socket.BeginReceive pins the buffer object that you pass in. ... The real fix is to allocate a fixed pool of buffers early during ...
    (microsoft.public.dotnet.framework)