From: aslan on
The following code compiles (and runs) OK with VC++ 6.

std::vector<bool> smallsieve;

smallsieve.reserve(smsize+1);

memset(smallsieve.begin(), true, smsize+1);



However I get the following error for the memset line.

1>c:\users\aslan\documents\visual studio
2005\projects\projecteuler\projecteuler\projecteuler.cpp(63) : error C2664:
'memset' : cannot convert parameter 1 from 'std::_Vb_iterator<_MycontTy>' to
'void *'
1> with
1> [
1> _MycontTy=std::vector<bool,std::allocator<bool>>
1> ]
1> No user-defined-conversion operator available that can perform
this conversion, or the operator cannot be called

How can I fix it?

Aslan


From: Igor Tandetnik on
aslan <aslanski2002(a)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

From: Ulrich Eckhardt on
aslan 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);

Well, the code is broken in several ways:

1. An iterator is not a pointer.
2. reserve() doesn't change the number of elements in a vector, you mean
resize().
3. memset() is the wrong tool for this anyway.

Try this

std::vector<bool> smallsieve(smsize+1, true);

to get a vector with 'smsize+1' elements that are all 'true'.

BTW: The C++ standard explicitly allows compressing each element of a
vector<bool> into a single bit, which makes the portable use of memset()
impossible. With all others, you could use memset(), however ugly and
unnecessary that is.

Uli

--
C++ FAQ: http://parashift.com/c++-faq-lite

Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
From: aslan on

"Igor Tandetnik" <itandetnik(a)mvps.org>, iletisinde �unu yazd�,
news:%23$muyYqcKHA.1640(a)TK2MSFTNGP06.phx.gbl...
aslan <aslanski2002(a)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

---

Thanks a lot.

It doesn't create any problem with the VC6 and runs OK. Yes it's weird but
believe it or not, memset doesn't write any random memory. Anyway I need to
rewrite some pieces as you pointed out.

Aslan

From: Igor Tandetnik on
"aslan" <aslanski2002(a)yahoo.com> wrote in message news:%23obrfjqcKHA.1028(a)TK2MSFTNGP06.phx.gbl...
> It doesn't create any problem with the VC6 and runs OK. Yes it's weird but
> believe it or not, memset doesn't write any random memory.

How do you determine that?
--
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