Boost logo

Boost Users :

Subject: Re: [Boost-users] Compress to buffer
From: Gavin Lambert (gavinl_at_[hidden])
Date: 2014-01-15 00:14:58


On 15/01/2014 14:46, Quoth Kenneth Adam Miller:
>
> From what I had thought though, if my string was empty with
> 0's,
>
>
> Right here's your mistake. string(30000,0) creates a non empty
> string. It's a string of 30k nulls.
[...]
>
> Ok, so supposing I just use a vector; do I use vector<byte>? Is it more
> efficient to use vector< word_size_of_computer > since any write to it
> would obviously be faster or will the compiler catch this? Also, when I
> grab all of the bytes from vector, does it automatically detect when the
> end is? Like, one of the purposes of having 0's all through the string
> was so that it was always terminated. In the case of vector, if I
> push_back 3 times, and then I output, will I get 3 bytes?

You can still use a string if you want, but you're not initialising the
pool correctly.

   std::string *p1 = new std::string(30000, 0);

This declaration creates a string that is 30k characters long, where all
characters happen to be 0. (It is legal for a string to contain
embedded nulls.) If you appended to this string, you would have a 30k+1
character string, and it *might* do a memory allocation. This is
obviously not what you want.

   std::string *p2 = new std::string();
   p2->reserve(30000);

This declaration creates an empty string with an initial capacity of at
least 30k characters; if you append a char to it then the string will be
1 char long. It should not allocate any further memory unless you try
to append more characters than that. (Though test it, as the STL is
permitted to ignore this.)

Also string will automatically ensure that it is always null-terminated
when viewed from .c_str(), so you normally don't need to worry about
filling with zeroes or anything like that, and you should not try to
append a zero yourself unless you actually want that in the string data.

But since you're talking about compressed data here, it will no longer
be printable characters and could potentially contain a 0 as actual
data. Either vector or string can cope with this just fine (vector is
typically more efficient at appending, but less efficient at copying)
but either way you'll need to use the .size() to determine how much data
is actually in there instead of relying on any termination.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net