From: David Wilkinson on 1 Dec 2009 12:17 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); > > 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: There are a lot of things wrong here: 1. Your code assumes that vector iterators are pointers, which they are in VC6, but not in later versions. 2. You are using reserve() when you should be using resize() 3. You are assuming that bool is the same size as char (and that vector<bool> is implemented in a straightforward way -- see below). Do like this int smsize = 10; std::vector<bool> smallsieve(smsize+1, true); There is also the issue that there is (in some compilers) something special about the way vector<bool> is implemented. See http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=98 Because vector<bool> seems so messed up I always use vector<int>. -- David Wilkinson Visual C++ MVP
From: aslan on 1 Dec 2009 12:23 "Ulrich Eckhardt" <eckhardt(a)satorlaser.com>, iletisinde şunu yazdı, news:nfufu6-lht.ln1(a)satorlaser.homedns.org... > 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. OK. Maybe it's my habit from VC++6. because it works with it (at least for std::vector<T>::begin()). > 2. reserve() doesn't change the number of elements in a vector, you mean > resize(). No it's reserve(). Again it's OK with VC++6. Yeah right, it's bad. I need to change it. > 3. memset() is the wrong tool for this anyway. bool happens to be 8-bit integer in VC++6 case, so again it's working there. If it's a bitmap in VC++ 2005 EE, you're right. > > Try this > > std::vector<bool> smallsieve(smsize+1, true); I did. > > 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. OK > > Uli > > -- > C++ FAQ: http://parashift.com/c++-faq-lite > > Sator Laser GmbH > Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932 Thanks a lot.
From: aslan on 1 Dec 2009 12:32 "Igor Tandetnik" <itandetnik(a)mvps.org>, iletisinde �unu yazd�, news:ejWBQoqcKHA.4780(a)TK2MSFTNGP04.phx.gbl... "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? *** aslan *** Using debugger's memory window (I'm talking about VC++6). -- 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: aslan on 1 Dec 2009 12:43 "David Wilkinson" <no-reply(a)effisols.com>, iletisinde şunu yazdı, news:uxFH5oqcKHA.5156(a)TK2MSFTNGP04.phx.gbl... > 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); >> 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: > > There are a lot of things wrong here: > > 1. Your code assumes that vector iterators are pointers, which they are in > VC6, but not in later versions. OK. > > 2. You are using reserve() when you should be using resize() I prefer reserve() in this case because it allocates a big chunk of memory at once. Then you can call resize() (many times) of course but without any reallocation taking place unless resize is called with a bigger size than the reserve() has been called. Or again am I wrong here? I describe what happens with VC++6. > > 3. You are assuming that bool is the same size as char (and that > vector<bool> is implemented in a straightforward way -- see below). > > Do like this > > int smsize = 10; > std::vector<bool> smallsieve(smsize+1, true); > > There is also the issue that there is (in some compilers) something > special about the way vector<bool> is implemented. See > > http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=98 > > Because vector<bool> seems so messed up I always use vector<int>. Thanks. I didn't bother with that because I mostly work with VC++6 and it's working fine there. > > -- > David Wilkinson > Visual C++ MVP Many thanks. Aslan
From: Igor Tandetnik on 1 Dec 2009 13:13
aslan <aslanski2002(a)yahoo.com> wrote: > "Igor Tandetnik" <itandetnik(a)mvps.org>, iletisinde þunu yazdý, > news:ejWBQoqcKHA.4780(a)TK2MSFTNGP04.phx.gbl... > "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? > > *** aslan *** Using debugger's memory window (I'm talking about > VC++6). Well, this way you see that some memory is being written to. How do you know that this memory is actually properly allocated and owned by the vector object? After all, if you just take an uninitialized pointer and memset to it, then the memory window will show that the memory pointed to by this pointer is in fact initialized - but that doesn't make the program valid. -- 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 |