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

Addressing a &vector<>[i] as a C-array & the C++ standard.

P: n/a
From the C++-FAQ Lite:

http://www.parashift.com/c++-faq-lit....html#faq-34.3

----------------------------
34.3] Is the storage for a std::vector<Tguaranteed to be contiguous? Yes.

This means you the following technique is safe:

#include <vector>
#include "Foo.h" /* get class Foo */

// old-style code that wants an array
void f(Foo* array, unsigned numFoos);

void g()
{
std::vector<Foov;
...
f(v.empty() ? NULL : &v[0], v.size()); ← safe
}

The funny expression v.empty() ? NULL : &v[0] simply passes the NULL
pointer if v is empty, otherwise passes a pointer to the first (zeroth)
element of v. If you know a priori that v is not empty, you can change
that to simply &v[0].
----------------------------

I can find nothing in the standard to justify this statement. Does
anyone know the section in the standard (if any) which justifies this
statement?

Thanks,

Tim S.
Sep 24 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Numeromancer wrote:
....
I can find nothing in the standard to justify this statement. Does
anyone know the section in the standard (if any) which justifies this
statement?
It is somthing ratified as errata I believe. I heard the next standard
will go as far as saying that basic_string is also contiguous.

I have no concrete reference other than postings by others.
Sep 24 '07 #2

P: n/a
Numeromancer wrote:
....
// old-style code that wants an array
void f(Foo* array, unsigned numFoos);

void g()
{
std::vector<Foov;
...
f(v.empty() ? NULL : &v[0], v.size()); ← safe
}

The funny expression v.empty() ? NULL : &v[0] simply passes the NULL
pointer if v is empty, otherwise passes a pointer to the first (zeroth)
element of v. If you know a priori that v is not empty, you can change
that to simply &v[0].
----------------------------

I can find nothing in the standard to justify this statement. Does
anyone know the section in the standard (if any) which justifies this
statement?
As I understand it if the vector is empty then any attempt to access any
element (in this case the first, at position 0) is passed the end of the
vector. Therefore this is undefined behaviour. This ternary operation
ensures that we never hit that situation.

Alan
Sep 24 '07 #3

P: n/a
Gianni Mariani wrote:
Numeromancer wrote:
...
>I can find nothing in the standard to justify this statement. Does
anyone know the section in the standard (if any) which justifies this
statement?

It is somthing ratified as errata I believe. I heard the next standard
will go as far as saying that basic_string is also contiguous.

I have no concrete reference other than postings by others.
Vector is contiguous as of TC1 aka the 2003 revision.
Sep 24 '07 #4

P: n/a
Numeromancer wrote:
From the C++-FAQ Lite:

http://www.parashift.com/c++-faq-lit....html#faq-34.3

----------------------------
34.3] Is the storage for a std::vector<Tguaranteed to be contiguous?
Yes.

This means you the following technique is safe:

#include <vector>
#include "Foo.h" /* get class Foo */

// old-style code that wants an array
void f(Foo* array, unsigned numFoos);

void g()
{
std::vector<Foov;
...
f(v.empty() ? NULL : &v[0], v.size()); ← safe
}

The funny expression v.empty() ? NULL : &v[0] simply passes the NULL
pointer if v is empty, otherwise passes a pointer to the first (zeroth)
element of v. If you know a priori that v is not empty, you can change
that to simply &v[0].
----------------------------

I can find nothing in the standard to justify this statement. Does
anyone know the section in the standard (if any) which justifies this
statement?

Thanks,

Tim S.
Thanks to those whose pointed to the TRs, I found it. I confess: I
thought that the author of the FAQ list was being chummy, (:humble but I
was wrong). I'm glad I asked. I'll be reading over TR1 carefully
henceforth.
Tim S.
Sep 28 '07 #5

P: n/a
Numeromancer wrote:
>The funny expression v.empty() ? NULL : &v[0] simply passes the NULL
pointer if v is empty, otherwise passes a pointer to the first
(zeroth) element of v. If you know a priori that v is not empty, you
can change that to simply &v[0].
----------------------------

I can find nothing in the standard to justify this statement. Does
anyone know the section in the standard (if any) which justifies this
statement?

Thanks,

Tim S.

Thanks to those whose pointed to the TRs, I found it. I confess: I
thought that the author of the FAQ list was being chummy, (:humble but I
was wrong). I'm glad I asked. I'll be reading over TR1 carefully
henceforth.
That's TC1 (Technical Corrigendum 1), not TR1.
Sep 28 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.