Why it's impossible for compiler to implicitly cast vector<>::iterator
to void*?
I have:
void f(void *, size_t);
....
vector<int> v;
v.reserve(n);
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())? 12 15285
Hi
Raider wrote: Why it's impossible for compiler to implicitly cast vector<>::iterator to void*?
Because vector<T>::iterator _may_ be a T*, but need not be.
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())?
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
Raider wrote: Why it's impossible for compiler to implicitly cast vector<>::iterator to void*?
I have: void f(void *, size_t); ... vector<int> v; v.reserve(n); 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())?
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]);
Regards,
Ben
Raider wrote: Why it's impossible for compiler to implicitly cast vector<>::iterator to void*?
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.
I have: void f(void *, size_t); ... vector<int> v; v.reserve(n); 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())?
Yeah.
Ben Pope
--
I'm not just a number. To many, I'm known as a string...
> That's like asking why it can't convert the vector to a void pointer.
I thought that something like operator T*() exists for
vector<T>::iterator.
Thanks!
> It would work (unless v.size() == 0 ...
I have v.reserve(n) to avoid this ;-)
Raider wrote: That's like asking why it can't convert the vector to a void pointer.
I thought that something like operator T*() exists for vector<T>::iterator.
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
--
I'm not just a number. To many, I'm known as a string...
Raider wrote: It would work (unless v.size() == 0 ...
I have v.reserve(n) to avoid this ;-)
reserve affects the capacity, not the size. ;-) Taking &v[0] when v.empty()
at best invokes undefined behaviour.
Jeff Flinn
"Raider" <sr*****@yandex.ru> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com... It would work (unless v.size() == 0 ...
I have v.reserve(n) to avoid this ;-)
Have you tested v.size() after calling reserve?
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 http://www.sgi.com/tech/stl/Vector.html
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.
On 2006-02-07 09:10:19 -0500, "Raider" <sr*****@yandex.ru> said: It would work (unless v.size() == 0 ...
I have v.reserve(n) to avoid this ;-)
It is still undefined behavior.
--
Clark S. Cox, III cl*******@gmail.com
On 2006-02-07 09:09:13 -0500, "Raider" <sr*****@yandex.ru> said: That's like asking why it can't convert the vector to a void pointer.
I thought that something like operator T*() exists for vector<T>::iterator.
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 cl*******@gmail.com
"Raider" <sr*****@yandex.ru> wrote in news:1139323723.343969.116000
@g44g2000cwa.googlegroups.com: SGI says (btw, where can I find C++ standard?) for STL vector: void reserve(size_type n)
See the FAQ (http://www.parashift.com/c++-faq-lite), section 6.13.
If n is less than or equal to capacity(), this call has no effect. Otherwise, it is a request for allocation of additional memory http://www.sgi.com/tech/stl/Vector.html
Allocates memory, but doesn't mean that the memory contains valid objects.
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.
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
well. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Kristofer Pettijohn |
last post by:
I'm hoping someone can help me understand the vector::pointer..
What is the correct way to go about going from:
std::vector<char> myCharVector(30);
to a char* ? (bad example, but char...
|
by: Dave |
last post by:
I'm crossposting this to both comp.lang.c++ and gnu.gcc because I'm not
sure if this is correct behavior or not, and I'm using the gcc STL and
compiler.
When calling vector<int>::push_back(0),...
|
by: Jim Luedtke |
last post by:
Can someone help me understand what might be going wrong here?
I'm writing a class template that looks something like this;
/* myclass.h */
#include<vector>
using namespace std
template...
|
by: Johan Pettersson |
last post by:
Hi,
I'm trying to "port" a project from VC++ 2003 to VC++ 2005 (Express
Edition). This project contains the following code on several places (It is
not exactly this code but a generalization of...
|
by: Daniel.Wyatt |
last post by:
I need some help with this please. Here's the important part:
#include <vector>
#include <string>
................
template <class T>
bool ResourceManager<T>::isLoaded(const std::string...
|
by: propokergrad |
last post by:
Hello, say I have two classes:
class Base{...};
class Derived : public Base{...}
I would like to do something similar to this:
std::vector<Base*>::iterator b;...
|
by: T. Crane |
last post by:
Hi all,
I have some data that I am using a vector<vector<double container
to hold. The data is n sets of (x,y,z,intensity) data points. I can
either group my data like this:
...
|
by: JackC |
last post by:
Hi,
If i have a std::vector containing pointers, then iterate through the
vector and im slightly confused as to how i use the item from the
iterator?
for example,
vector<MyClass*myvector;...
|
by: Ralf Goertz |
last post by:
Hi,
the following templated code doesn't compile. It gives the error:
template_it.cc:17: error: type 'std::vector<Derived1<T>*,
std::allocator<Derived1<T>*' is not derived from type...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
| |