does all implementations of std::vector use regular arrays?

S

slyi

Is it ok to assume that the following assertion is valid for all
implementations of std::vector?:

std::vector<T> v(10);
T* p = &v[0];
for (size_t n=0; n < v.size(); n++)
assert( p+n == &v[n] );

or is it possible (does the standard allow) for an implementation to
use some weird internal storage scheme for the contained random access
sequence?

thanks,
slyi
 
L

Luke Meyers

I'm not a standard buff, but I can think of plenty of plausible vector
implementations which would break your code snippet in a hurry. Use
the interface, that's what it's there for. If you want an array, use
an array.

Is this an entirely academic question, or did you have some intended
use for this... deviant behavior? ;)

Luke
 
B

Bob Hairgrove

Is it ok to assume that the following assertion is valid for all
implementations of std::vector?:

std::vector<T> v(10);
T* p = &v[0];
for (size_t n=0; n < v.size(); n++)
assert( p+n == &v[n] );

or is it possible (does the standard allow) for an implementation to
use some weird internal storage scheme for the contained random access
sequence?

No. Except for vector<bool>, the above code is required by the C++
standard to work (see section 23.2.4).
 
D

deane_gavin

slyi said:
Is it ok to assume that the following assertion is valid for all
implementations of std::vector?:

std::vector<T> v(10);
T* p = &v[0];
for (size_t n=0; n < v.size(); n++)
assert( p+n == &v[n] );

or is it possible (does the standard allow) for an implementation to
use some weird internal storage scheme for the contained random access
sequence?

std::vector is guranteed to store its memory in one contiguous chunk so
your assertion will be valid for all (conforming) implementations of
std::vector. The guarantee of contiguousness (contiguity?) compatible
with an array came in with the 2003 standard so in principle, your
assertion could be invalid on a C++98 conforming implementation.
However, this article by Herb Sutter

http://www.gotw.ca/publications/mill10.htm

from 1999 suggests that in practice you would still be OK.

One of the important reasons for making that guarantee explicit is so
that code like this can work

// This function (and countless others like
// it) exists in a legacy API and works with
// data in an array.
// The data is not modified by the function.
void f(const int* data, size_t length)
{
// ...
}

I can safely do

std::vector<int> v(10);

// I can write modern style C++ throughout
// my new code, but still use the legacy API.
f(&v[0], v.size());

Gavin Deane
 
S

slyi

Thanks all for the references. One thing Gavin:

Why not pass &v[0] to a function that does modify the data?


slyi
 
B

BobR

slyi wrote in message
Thanks all for the references. One thing Gavin:

Why not pass &v[0] to a function that does modify the data?
slyi

Because you don't need to!

#include <iostream>
#include <ostream> // std::endl
#include <vector>

void InitVector( std::vector<int> &vec){
for(size_t i(0); i < vec.size(); ++i){
vec.at( i ) = int( i );
} // for(i)
return;
} // InitVector( std::vector<int>&)

int main(){
std::vector<int> v(10);
InitVector( v );
for(size_t i(0); i < v.size(); ++i){
std::cout<<v.at( i )<<std::endl;
} // for(i)
return 0;
} // main() end

Mr. Deane was just showing you that you could call legacy code with a vector,
where an array ( int const * ) was expected. It just happened to be 'const'.
 

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

Forum statistics

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

Latest Threads

Top