Re: Type cast problem with VC++ 2005 Express Edition
aslan <aslanski2002@yahoo.com> wrote:
The following code compiles (and runs) OK with VC++ 6.
std::vector<bool> smallsieve;
smallsieve.reserve(smsize+1);
memset(smallsieve.begin(), true, smsize+1);
In VC6, vector<T>::iterator happens to be defined as simply T*. Your =
program improperly relies on this implementation detail.
Further, I'm not sure your code actually ever worked - it might just =
appear to. You see, vector<bool> is a specialization of the general =
vector<T> template which packs its values into individual bits. It does =
not internally contain an array of bool. Chances are, your memset writes =
over some random memory, and it's only by accident that your program =
doesn't eventually crash.
Also, vector::reserve doesn't change the size of the vector (size() =
still returns 0), only its capacity. In light of this, I don't =
understand the point of this code at all.
If you want to construct a vector with a given number of elements all =
initialized to the same value, just make it
std::vector<bool> smallsieve(smsize+1, true);
// or
std::vector<bool> smallsieve;
smallsieve.assign(smsize+1, true);
// or
std::vector<bool> smallsieve;
smallsieve.insert(smallsieve.end(), smsize+1, true);
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not =
necessarily a good idea. It is hard to be sure where they are going to =
land, and it could be dangerous sitting under them as they fly overhead. =
-- RFC 1925