vector directly !

Started by
9 comments, last by Xai 18 years, 4 months ago
I heard of someone said , you should not access directly stl vector.it is dangous. but I see code below in a good book. std::vector<BYTE> in (num); ifstream infile(filname,std::io_base::binary) if (infile == 0)return; inflie.read(&in[0],in.size); it is correct?
Advertisement
The code is correct (though I tend to prefer &in.front() instead of &in[0], but that's just a matter of taste I guess). The thing here is that the vector cells are contiguous in memory, thus you can access to the data like this. This is usefull when you mix C++ and some legacy C code.

But of course, there is a C++ friendly way to do this: using stream iterators, and the std::copy() function.

HTH,
That code is fine, and really not dangerous at all. What the problem is, is when you take a pointer to the vector and start messing around with it that way. That's what they meen by direct access. Observe:

stl::vector<int> mysample;int* mypointer = mysample.pointer;mypointer = new int[100];
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
Quote:Original post by dbzprogrammer
*** Source Snippet Removed ***

I fail to understand your pseudo code. You seem to imply - wrongly - that reassigning a pointer that is already pointing to a vector can somehow modify the vector itself. If you meant that changing the pointer of a (dynamically allocated) vector without deleting it first could lead to resource leaks then you'd be correct.

Could you explain this more?

BTW: you cannot assign a pointer to int the address of a std::vector, and the namespace for the standard library is "std".
:stylin: "Make games, not war.""...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
It's not supposed to really make sense, or be correct. But the problem with that is that you could do things with the pointer that could mess up the vector. Maybe static_cast stuff? I really don't care what, but the fact that you can. Sorry about the typo... And there is a member in the std::vector<> class called pointer which contains a Type* pointer to the first element of the vector.
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
Quote:Original post by dbzprogrammer
And there is a member in the std::vector<> class called pointer which contains a Type* pointer to the first element of the vector.

What implementation are you using? In mine, pointer is a typedef for a pointer to element. As such, it doesn't point to anything.
:stylin: "Make games, not war.""...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
vector<int> mysample;int* mypointer = mysample.pointer;mypointer = new int[100];


hmm, more dangerous to me would be

vector<Stuff> vec;fill( vec );char * pointer = (char*)&vec;for( int i = 0 ; i < sizeof( vector ) ; ++i )    pointer = (char)rand();


you really dont want to be doing that though...
it is correct to do whatever you want to do to the data in the continous buffer managed by the vector, it is incorrect to do anything at all to the buffer itself, and also incorrect to do anything before or after the first and last VALID element in the buffer currently.

To be clear assume a vector of int's has a capacity of 100 and a current size of 54. It is valid for you to write whatever you want to bytes in this range:

&myVector[0] - (&myVector[53] + sizeof(int) - 1)

it is invalid to write to any other bytes, or to do any operation such as new/delete or malloc/realloc/free.
I also didn't make clear, you cannot HOLD that pointer to the buffer ... it may move when the vector resizes ... you may only use the address of the buffer to do things between calls to modifying operations.
Quote:Original post by Xai
it is correct to do whatever you want to do to the data in the continous buffer managed by the vector, it is incorrect to do anything at all to the buffer itself, and also incorrect to do anything before or after the first and last VALID element in the buffer currently.

To be clear assume a vector of int's has a capacity of 100 and a current size of 54. It is valid for you to write whatever you want to bytes in this range:

&myVector[0] - (&myVector[53] + sizeof(int) - 1)

it is invalid to write to any other bytes, or to do any operation such as new/delete or malloc/realloc/free.

what is the sizeof(int) for? Whatever the case, it is probably unneccessary...pointer arithmetic automatically adjusts for sizes, so you don't have to do so manually unless you're dealing with void* for some crazy reason.

*edit: By the way, a more straight forward range:
[&myVector.front(), &myVector.back()]


CM

[Edited by - Conner McCloud on November 30, 2005 9:23:22 PM]

This topic is closed to new replies.

Advertisement