vector::iterator to a pointer casting

Discussion in 'C++' started by Raider, Feb 7, 2006.

  1. Raider

    Raider Guest

    Why it's impossible for compiler to implicitly cast vector<>::iterator
    to void*?

    I have:
    void f(void *, size_t);
    vector<int> v;
    f(v.begin(), n * sizeof(int));

    And compiler (VC7.1) tells me that it unable to convert v.begin() to
    void*. What's the easiest way to do it? &(*v.begin())?
    Raider, Feb 7, 2006
    1. Advertisements

  2. Raider

    Markus Moll Guest

    It would work (unless v.size() == 0, i.e. v.begin() == v.end()), but I think
    it's easier and more common to use &v[0].

    Markus Moll, Feb 7, 2006
    1. Advertisements

  3. Raider

    benben Guest

    You will find out that C++ generally does not allow you to implicitly
    cast just about any type to void*, period.

    But if you insist, you can do it the following rather ugly code:

    void* pt = static_cast<void*>(&v[0]);

    benben, Feb 7, 2006
  4. Raider

    Ben Pope Guest

    because vector<>::iterator is not a pointer.

    That's like asking why it can't convert the vector to a void pointer.

    The iterator has pointer syntax, and much of pointer semantics, but that
    doesn't mean it IS a pointer.

    Ben Pope
    Ben Pope, Feb 7, 2006
  5. Raider

    Raider Guest

    That's like asking why it can't convert the vector to a void pointer.

    I thought that something like operator T*() exists for

    Raider, Feb 7, 2006
  6. Raider

    Raider Guest

    It would work (unless v.size() == 0 ...

    I have v.reserve(n) to avoid this ;-)
    Raider, Feb 7, 2006
  7. Raider

    Duane Hebert Guest

    Have you tested v.size() after calling reserve?
    Duane Hebert, Feb 7, 2006
  8. Raider

    Ben Pope Guest

    You'd probably get away with it for vector<T>::iterator, but not all
    iterators support everything that a pointer does, so it would introduce
    an asymmetry between say, vector and list.

    Ben Pope
    Ben Pope, Feb 7, 2006
  9. Raider

    Jeff Flinn Guest

    reserve affects the capacity, not the size. ;-) Taking &v[0] when v.empty()
    at best invokes undefined behaviour.

    Jeff Flinn
    Jeff Flinn, Feb 7, 2006
  10. Raider

    Raider Guest

    SGI says (btw, where can I find C++ standard?) for STL vector:
    void reserve(size_type n)
    If n is less than or equal to capacity(), this call has no effect.
    Otherwise, it is a request for allocation of additional memory

    Ok, size() == 0 but I think capacity() will be enough to use v[0] up to
    v[n-1]. Is it needs to use resize()??? I don't think so.
    Raider, Feb 7, 2006
  11. It is still undefined behavior.
    Clark S. Cox III, Feb 7, 2006
  12. No, iterators are not necessarily convertible to pointers. If you've
    been relying on this behavior, then your code is broken (i.e. it may
    work with some compilers, but not with others).
    Clark S. Cox III, Feb 7, 2006
  13. Raider

    Andre Kostur Guest

    See the FAQ (, section 6.13.
    Allocates memory, but doesn't mean that the memory contains valid objects.
    Yes, you must use resize. While you might be lucky, and it may work for a
    vector of ints, it won't work for vector of class (like std::string). Also
    if you're using a checked/debug version of the STL, it may enforce this as
    Andre Kostur, Feb 7, 2006
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.