This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: OT: std::vector to C array


Hi Sebastian,

Approach #1 is the correct way to convert a std::vector to a C array.

Approach #2 does not convert the std::vector to a C array. It merely sets a pointer to the begin iterator in those cases where vector<T>::iterator is incidently identical to the pointer, and (I assume) causes a compiler error in other cases.

Approach #3 does not convert the std::vector to a C array. It merely sets a pointer to the first element. Note: std::vector<> does NOT have a front() method, the std::queue<> does. You should rather do something like: a = &(*v.begin()); ... with the caveat that you are relying on your STL implementation to utilize consecutive memory.

I believe that the current ISO 14882 standard does NOT guarantee that a std::vector<> is necessarily in consecutive memory, so using a pointer to do C-style pointer/array access is not guaranteed to work.

However, all implementations of std::vector<> that I am aware of DO implement vector elements in consecutive memory. (But my awareness is not omniscient. *heh*)

Also, I believe that the working group for the future ISO 14882 standard is seriously considering the guarantee that std::vector<> (and likewise with std::string<>) is in consecutive memory locations.

Sincerely,
--Eljay


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]