• Advertisement
Sign in to follow this  

in the vector in stl.

This topic is 4651 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

vector<BYTE> in (4096); and the pointer of in head is not the in? I must use &in[0] to get the head pointer?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
what pointer? what head? what does the 4096 mean?

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
what pointer? what head? what does the 4096 mean?


I wonder if he's got confused between vectors and linked lists?

Share this post


Link to post
Share on other sites
I mean first address of a vector/list is just the name of vector.

but vector in stl may be exception.

Share this post


Link to post
Share on other sites
Yes, you'll most likely need to take the address of the first element.

I don't think STL vector has an overload for the cast operator... I shouldn't say for sure, try and fail, and tell us if I'm wrong.

Share this post


Link to post
Share on other sites
Correct.

Since vector<> is a class and doesn't provide an overloaded operator for such a pointer, you actually need to write &in[0] to obtain a pointer to the first element of the vector.

-Markus-

Share this post


Link to post
Share on other sites
There is (yet) no guarantee that the elements in a std::vector are allocated in a continuous chunk of memory (according to the C++ standard), but in practice this is true in all implementations (except for the ugly std::vector<bool> specialization, which shouldn't be used anyway). One thing to take extra care about is an empty std::vector, these (somewhat handy) functions can perhaps be of some help:

template <typename T, typename A>
inline const T* c_ptr(const std::vector<T, A>& v)
{
return v.empty() ? 0 : &v[0];
}

template <typename T, typename A>
inline T* c_ptr(std::vector<T, A>& v)
{
return v.empty() ? 0 : &v[0];
}

Share this post


Link to post
Share on other sites
I don't understand why that code is useful... What's wrong with &vec[0] on an empty vector? You are likely going to query the vector's size anyway, so you would discover that the size is zero...



http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html

Item 69

Status: TC - (Technical Corrigenda) - The full WG21 committee has voted to accept the Defect Report's Proposed Resolution as a Technical Corrigenda. Action on this issue is thus complete and no further action is possible under ISO rules.

Share this post


Link to post
Share on other sites
Quote:
Original post by RDragon1
What's wrong with &vec[0] on an empty vector?

int* p = NULL; // Legal.
int& r = *p; // Illegal.

vec[0] returns a reference to the first element in the vector, and what is that if the vector is empty?

Share this post


Link to post
Share on other sites
Quote:
Original post by dalleboy
There is (yet) no guarantee that the elements in a std::vector are allocated in a continuous chunk of memory (according to the C++ standard)


As of C++ 2003 Technical Corrigendum 1 (TC1) elements of std::vector are guaranteed to be stored contiguously.

Quote:
Original post by dalleboy
except for the ugly std::vector<bool> specialization, which shouldn't be used anyway


Nothing wrong with using it, it just has issues [smile].

Share this post


Link to post
Share on other sites
Quote:
Original post by snk_kid
Nothing wrong with using it, it just has issues [smile].

As long as you know the issues, sure... [wink]
<JamesEarlJonesVoice>It is an abomination and must be destroyed!</JamesEarlJonesVoice>

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement