Just how bad is this code?

B

Bob

#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;
}
 
V

Victor Bazarov

Bob said:
#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];
^^^^^^^^^^^^^^
I don't think this like is going to work. You cannot initialise
a bool& with std::vector said:
std::memcpy(ba, &c, sizeof(bool) * v.size());

delete [] ba;
}
}

int main()
{
foo();

Did you mean

bad();

??
return 0;
}


Victor
 
B

Bob Hairgrove

#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.
 
V

Victor Bazarov

Bob said:
#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()??

The definition is probably doesn't matter. No declaration should
post the problem, though.
Why is bad() contained in an anonymous namespace?

Why not?
Why do you need <cstring>?

Because of 'std::memcpy', probably.

Victor
 
C

chris

Bob said:
#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;
}

Actually, this code would probably be alright with a vector of anything
except booleans (I don't know if you knew this or not), although I don't
think the standard promises it (although there is a good chance it will
in a future version, and it would in every implementation I've ever seen)

Unfortunatly (at least in my opinion) vector<bool> is specialised to a
"more efficent" implementation where the booleans are stored in a
compact form, so this kind of copying won't work. In fact even bool&
c=v[0] won't work.

If all you want to do is copy an array of booleans into a vector, and
you don't do it too often, then just use std::copy.

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top