On 20 Sep 2004 09:18:59 -0700,
xa******@hotmail.com (Bob) wrote:
#include <vector>
#include <cstring>
namespace
{
void bad()
{
std::vector<bool> v(0);
v.push_back(true);
v.push_back(false);
v.push_back(true);
bool b = v[0];
bool* ba = new bool[v.size()];
bool& c = v[0];
std::memcpy(ba, &c, sizeof(bool) * v.size());
delete [] ba;
}
}
int main()
{
foo();
return 0;
}
Hmmm ...
Where is the definition of foo()??
Why is bad() contained in an anonymous namespace?
Why do you need <cstring>?
As to using memcpy to copy vector<bool> to a C array, don't do it.
It's not guaranteed to work because vector<bool> is likely to be
specialized by the implementation of the STL to use something other
than bool internally.
For any other type, though, it should work because storage for a
vector must be allocated contiguously in memory according to the
newest version of the C++ standard, and all the most widely used
implementations have implemented vector<> that way before, anyway.
Instead of memcpy, I would recommend using a loop and assigning each
element of the array directly. It's probably just as fast.
--
Bob Hairgrove
No**********@Home.com