473,471 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Just how bad is this code?

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;
}
Jul 22 '05 #1
4 1739
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]; ^^^^^^^^^^^^^^
I don't think this like is going to work. You cannot initialise
a bool& with std::vector<bool>::reference.

std::memcpy(ba, &c, sizeof(bool) * v.size());

delete [] ba;
}
}

int main()
{
foo();
Did you mean

bad();

??
return 0;
}

Victor
Jul 22 '05 #2
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
Jul 22 '05 #3
Bob Hairgrove wrote:
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()??


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
Jul 22 '05 #4
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;
}


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
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Steven Burn | last post by:
Just wondering if it would be possible to convert an existing e-mail form to; 1. Send the e-mail (as it does now...without problems) 2. Copy one folder on the server, to another new folder ...
99
by: Jim Hubbard | last post by:
It seems that Microsoft not only does not need the classic Visual Basic developer army (the largest army of developers the world has ever seen), but now they don't need ANY Windows developer at a...
11
by: Phill | last post by:
I'm new to C# and so far I'm pretty happy with the language and the design behind it. My only real concern is about performance. I wanted to ask a question about how a program you build in Visual...
16
by: John Rivers | last post by:
http://www.15seconds.com/Issue/030812.htm?voteresult=1 poor guy worked his heart out, just to make a page control and then they published it ha ha ha ha ha to "help" others
16
by: LP | last post by:
Hello, I am trying to use .NET with Excel. I installed Office 2003 and selected ..NET programming suport option, so it installed all those PIA, as MS sugests. But I can not find a way to destroy...
85
by: Russ | last post by:
Every Python programmer gets this message occasionally: IndexError: list index out of range The message tells you where the error occurred, but it doesn't tell you what the range and the...
1
by: Ronald S. Cook | last post by:
If I choose not to include a dependent project in my solution, but rather just reference the .dll in that project's bin\debug folder, are there any negative repercussions? Or, is it more...
31
by: Sam of California | last post by:
Is it accurate to say that "the preprocessor is just a pass in the parsing of the source file"? I responded to that comment by saying that the preprocessor is not just a pass. It processes...
7
by: Sin Jeong-hun | last post by:
Hi. I need to simulate keyboard and mouse events. For the first step, I tried to move mouse using SendInput. But it didn't work. I searched all over the internet for an example, but only questions...
2
by: RHNShK | last post by:
SQL> ED Wrote file afiedt.buf 1 select ROUND(avgprice, 10) avgprice, SeQ_no 2 from (select b.*, 3 sum(decode(INVXH_code, 'BUY', invxh_TOTAL_QTY, -invxh_TOTAL_QTY))...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.