Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Get TYPE* array from vector<TYPE> - is this OK?

Reply
Thread Tools

Get TYPE* array from vector<TYPE> - is this OK?

 
 
Goran Pusic
Guest
Posts: n/a
 
      09-07-2010
Hi all!

Given:
std::vector<TYPE> v;
void process(size_t count, TYPE* data);

Is there any problem if I do

if (!v.empty())
{
std::vector<TYPE>::reference r = *v.begin();
// or
r = v[0];
// or
r = v.at(0);
process_raw(v.size(), &p);
}

?

(This is what I do all the time to interface with lower-level code,
but I've always been wondering if there's some issue in doing things
like these).

TIA,

Goran.
 
Reply With Quote
 
 
 
 
Bart van Ingen Schenau
Guest
Posts: n/a
 
      09-07-2010
On Sep 7, 3:26*pm, Goran Pusic <gor...@cse-semaphore.com> wrote:
> Hi all!
>
> Given:
> std::vector<TYPE> v;
> void process(size_t count, TYPE* data);
>
> Is there any problem if I do
>
> if (!v.empty())
> {
> * std::vector<TYPE>::reference r = *v.begin();
> * // or
> * r = v[0];
> * // or
> * r = v.at(0);
> * process_raw(v.size(), &p);
>
> }
>
> ?


No problem at all. It was for supporting code like this that
std::vector always has had the guarantee that it uses contiguous
storage for it elements.

Bart v Ingen Schenau
 
Reply With Quote
 
 
 
 
Alf P. Steinbach /Usenet
Guest
Posts: n/a
 
      09-07-2010
* Bart van Ingen Schenau, on 07.09.2010 15:42:
> On Sep 7, 3:26 pm, Goran Pusic<gor...@cse-semaphore.com> wrote:
>> Hi all!
>>
>> Given:
>> std::vector<TYPE> v;
>> void process(size_t count, TYPE* data);
>>
>> Is there any problem if I do
>>
>> if (!v.empty())
>> {
>> std::vector<TYPE>::reference r = *v.begin();
>> // or
>> r = v[0];
>> // or
>> r = v.at(0);
>> process_raw(v.size(),&p);
>>
>> }
>>
>> ?

>
> No problem at all. It was for supporting code like this that
> std::vector always has had the guarantee that it uses contiguous
> storage for it elements.


Not "always"; it got that guarantee in C++03.


Cheers,

- Alf

--
blog at <url: http://alfps.wordpress.com>
 
Reply With Quote
 
Alf P. Steinbach /Usenet
Guest
Posts: n/a
 
      09-07-2010
* Marcel Müller, on 07.09.2010 15:49:
> Goran Pusic wrote:
>> Hi all!
>>
>> Given:
>> std::vector<TYPE> v;
>> void process(size_t count, TYPE* data);
>>
>> Is there any problem if I do
>>
>> if (!v.empty())
>> {
>> std::vector<TYPE>::reference r = *v.begin();
>> // or
>> r = v[0];
>> // or
>> r = v.at(0);
>> process_raw(v.size(), &p);

>
> p is undefined. You probably mean
> process_raw(v.size(), &*v.begin());


process_raw is undefined. You probably mean

process( v.size, &r );


>> }

>
> I remember that we had the same discussion some time ago. AFAIR the result was
> that the storage layout of vector is not guaranteed by the standard in this way,
> but this is more an accident rather then intensionally.


A std::vector is guaranteed to have contiguous storage, per C++03.


Cheers & hth.,

- Alf

--
blog at <url: http://alfps.wordpress.com>
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      09-08-2010
Goran Pusic wrote:
>
> Given:
> std::vector<TYPE> v;
> void process(size_t count, TYPE* data);
>
> Is there any problem if I do
>
> if (!v.empty())
> {
> std::vector<TYPE>::reference r = *v.begin();
> // or
> r = v[0];
> // or
> r = v.at(0);
> process_raw(v.size(), &p);
> }
>
> ?


Yes, the technique it is perfectly valid.

You can simply use `&v.front()` to obtain the pointer to the first
element of the raw array, instead of going through the iterators and all
these intermediate dereferences and address-takings.

Also, be aware of specialized versions of `std::vector<>`, like
`std::vector<bool>` for example, which will not work with this technique
(it stores bool values in a packed array of bits, providing no direct
access to the raw array).


--
Best regards,
Andrey Tarasevich
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const and array of array (of array ...) Mara Guida C Programming 3 09-03-2009 07:54 AM
Get ubuntu ! Get ubuntu ! Get ubuntu ! Get ubuntu ! Getubuntu Windows 64bit 1 06-01-2009 08:54 AM
Get Array From ByteBuffer (If ByteBuffer doesn't have an array backing it) res7cxbi@verizon.net Java 2 01-01-2006 08:58 PM
length of an array in a struct in an array of structs in a struct in an array of structs Tuan Bui Perl Misc 14 07-29-2005 02:39 PM
Length of Array of Array of Array Tom Perl Misc 3 12-20-2004 05:23 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57