Skip to main content
GameDev.net gamedev.net
🔒 Locked

casting int* to vectorint

Started by browny Jun 5, 2006 at 11:30 AM 6 replies 850+ views
Original Post
browny
browny
Well casting int* to vector used to work in VC++6, which doesn't work anymore in VC++7 or 8. I know its probably not the neatest thing to do for a C++ purist but some libraries would take int* as function arguments while we were dealing with vector... is there anyway we can do the casting or do i have to make a seperate malloc and them copy all the content of the vector to the allocated memory and bla bla... (damn, in that case.. it will take lots of time just to covert data)
Z
Enigma
Enigma
void c_api_requiring_int_pointer(int * pointer, unsigned int size);void my_function(){	std::vector< int > my_vector;	fill(my_vector);	assert(!my_vector.empty());	c_api_requiring_int_pointer(&my_vector[0], my_vector.size());}
Σnigma
Fruny
Fruny
Quote:
Original post by browny
Well casting int* to vector used to work in VC++6, which doesn't work anymore in VC++7 or 8.


int array[100];std::vector<int> vec(array, array+100); // copy the contents of the array.


Enigma has already demonstrated the vector -> array 'cast', though I prefer &vec.front() to &vec[0]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Zahlman
Zahlman
No, it didn't. Or at least it shouldn't have.

Anyway, the casting you want in the described situation is in the other direction. You cast "from" the current type "to" the required type. Anyway, your question has been answered.

Enigma, are you sure that assertion is necessary? If .empty(), .size() will be zero, and the C API should have been designed to handle the case of a zero length list...
mikeman
mikeman
Quote:

Enigma, are you sure that assertion is necessary? If .empty(), .size() will be zero, and the C API should have been designed to handle the case of a zero length list...


Yeah, but in that case &my_vector[0] would cause a subscript range error. If we don't assert(), we must at least check for zero length and send a NULL pointer to the C function or something.
Sharlin
Sharlin
This is an interesting dilemma. It would seem that there's no portable way to obtain a pointer to the contents of an empty vector. A proposal has been made, however, to add a data() (both const and non-const) member functions to std::vector, which will take care of this problem. (There's also the extremely handy at() member function for std::maps that throws if the given key was not found.) Technically, the addition is not yet part of the draft Standard, but its inclusion is pretty much certain. libstdc++, for example, already supports those functions.

[Edited by - Sharlin on June 5, 2006 2:06:00 PM]
Fruny
Fruny
Quote:
Original post by Sharlin
There's also the extremely handy at() member function for std::maps that throws if the given key was not found.


Which std::vector also has.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
NotAYakk
NotAYakk
Hey look, it gives a pointer to the vector's data if it exists:
template<typename Vector>Vector::value_type* beginof( Vector& v ) {  if (v.empty()) return 0;  return &v.front();}template<typename Vector>Vector::value_type const* beginof<const Vector>( Vector const& v ) {  if (v.empty()) return 0;  return &v.front();}


Improvements would include making sure you are passed a vector and not some other standard container.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.