By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,920 Members | 1,660 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,920 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Dec 29 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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

Dec 29 '05 #2

P: n/a
On 29 Dec 2005 01:51:44 -0800, "slyi" <sl*********@gmail.com> wrote:
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).

--
Bob Hairgrove
No**********@Home.com
Dec 29 '05 #3

P: n/a
slyi wrote:
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

Dec 29 '05 #4

P: n/a
I stand corrected (by other respondents). I always liked deviance,
anyway.

Dec 29 '05 #5

P: n/a
Thanks all for the references. One thing Gavin:

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

Dec 29 '05 #6

P: n/a

slyi wrote in message
<11**********************@g49g2000cwa.googlegroups .com>...
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'.

--
Bob R
POVrookie
Dec 30 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.