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

G

Goran Pusic

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.
 
B

Bart van Ingen Schenau

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
 
A

Alf P. Steinbach /Usenet

* Bart van Ingen Schenau, on 07.09.2010 15:42:
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
 
A

Alf P. Steinbach /Usenet

* Marcel Müller, on 07.09.2010 15:49:
Goran said:
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
 
A

Andrey Tarasevich

Goran said:
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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top