Re: Type cast problem with VC++ 2005 Express Edition

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




"Ulrich Eckhardt" <eckhardt@xxxxxxxxxxxxxx>, iletisinde şunu yazdı, news:d9jhu6-05v.ln1@xxxxxxxxxxxxxxxxxxxxxxxxx
aslan wrote:
"Ulrich Eckhardt" <eckhardt@xxxxxxxxxxxxxx>, iletisinde şunu yazdı,
news:nfufu6-lht.ln1@xxxxxxxxxxxxxxxxxxxxxxxxx
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.

Danger: reserve() allocates enough memory to store the given number of
elements, so you can add elements without the vector having to reallocate
memory. This does not change the size and it does not technically create
those elements or allow you to access them. Try this with VC6:

std::vector<bool> vec;
vec.reserve(2);
memset(smallsieve.begin(), true, 2);
bvec.push_back(false);
for(int i=0; i!=3; ++i)
std::cout << "vec[" << i << "]=" << vec[i] << std::endl;

You could also write a test class that gives you a message whenever it is
created, copied, assigned and destroyed and put that into a vector. You
could then actually see the difference between resize() and reserve().

Accessing them is what is called "undefined behaviour", which is standardese
for "you should have checked that yourself and all guarantees are off". You
are accessing these nonexistent elements using memset(). Since you are
probably never actually changing the size afterwards and the vector on its
own doesn't touch that memory, you never even notice.
OK.

BTW: I mentioned that you can actually use memset():

std::vector<T> vec;
vec.resize(n);
std::memset(&vec.front(), 0, vec.size()*sizeof(T));

This is almost what I did after the suggestions. But without memset because you can also specify the value to set to resize(), so:

std::vector<bool> vec;
vec.resize(n, true);


This requires that 'T' is a POD, i.e. a type without any constructor,
virtual function etc. Builtin types, enumerations, unions and C-compatible
structures are such PODs.

Uli

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

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

.



Relevant Pages

  • Re: Type cast problem with VC++ 2005 Express Edition
    ... could then actually see the difference between resize() and reserve. ... own doesn't touch that memory, ... But without memset because you can also specify the value to set to resize, ... // blah blah blah ...
    (microsoft.public.vc.language)
  • Re: Type cast problem with VC++ 2005 Express Edition
    ... this way you see that some memory is being written to. ... 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. ... So you have one big chunk of memory on which you can call resize() as many times as possible without any reallocation occurring provided that you don't resizewith a bigger size than reservehas already allocated. ...
    (microsoft.public.vc.language)
  • Re: Initialization of very large blocks of memory
    ... After that, we initialize that memory: ... Is there any way to manage performance ot the initializing (not necessarily using memset)? ... Since vallocexplicitly says it doesn't initialize and is managed by ... thread within the kernel... ...
    (comp.sys.hp.hpux)
  • Re: why it is not work?
    ... you use memset() to set memory pointed to by pointer data ... If pointer data is not initialized, ... If malloccan't allocate memory, it returns NULL, and dereferencing ...
    (comp.lang.c)
  • [PATCH v3] compiler: prevent dead store elimination
    ... secure_bzeroensures a section of memory is padded with zeroes. ... To prevent a memset() from being eliminated, ... the pragma directive to set the optimization level. ... optimization level is greater than 3, dead store removal occurs. ...
    (Linux-Kernel)