fortran array and std::vector<T>

S

sebastian sauer

hi,

1st i read josuttis' STL book. then i knew std::vector<T> is de-facto
guaranteed to be contiguous and this will be in the nxt revision of the
standard. all good. all fine.

then wikipedia tought me initial skecpticism. but after reading
http://en.wikipedia.org/wiki/Skepticism i knew only plato can be my
teacher. so i virtually made it my way to plato at stanford.
http://plato.stanford.edu/entries/skepticism/

trust none, question it all, take nothing for granted, search until you are
proven all you know is wrong, was my new motto.

...and i quickly found ..
http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html
http://www.parashift.com/c++-faq-lite/containers.html#faq-34.3

so now i'm lost in knowledge ;)

my case: a C++ std::vector<T> that is also accessed by a fortran subroutine
as an array.

my question: how shall i do it? what is morale? what is legal? and what is
practical?

void g()
{
std::vector<Foo> v;
...
f(v.begin(), v.size()); // this is what i cannot do i figure
f(&v[0], v.size()); // this is the yuk-dirty way i came up with
}


please enlighten me what is here the best thing to do?

cheers,
s.
 
V

Victor Bazarov

sebastian said:
[..]
my case: a C++ std::vector<T> that is also accessed by a fortran
subroutine as an array.

my question: how shall i do it? what is morale? what is legal? and
what is practical?

void g()
{
std::vector<Foo> v;
...
f(v.begin(), v.size()); // this is what i cannot do i figure
f(&v[0], v.size()); // this is the yuk-dirty way i came up with
}


please enlighten me what is here the best thing to do?

Out of relatively short ones, you've found probably the best way to
do it. The only comment I have: when I knew Fortran, all variables
were passed to it by the address, except the arrays which were passed
by the address of the first element. Of course, today's Fortran can
be different, and I will take your word that the latter call to 'f'
works. If it does, you're there. If it still doesn't, we cannot
really help since Fortran is beyond the scope of a C++ newsgroup.

V
 
K

Kai-Uwe Bux

sebastian said:
hi,

1st i read josuttis' STL book. then i knew std::vector<T> is de-facto
guaranteed to be contiguous and this will be in the nxt revision of the
standard. all good. all fine.

Your information is outdated: std::vector<T> already is required to be
contiguous (since 2003).

[23.2.4/1]

... The elements of a vector are stored contiguously, meaning that if v is
a vector<T, Allocator> where T is some type other than bool, then it obeys
the identity &v[n] == &v[0] + n for all 0 <= n < v.size().

[snip]
my case: a C++ std::vector<T> that is also accessed by a fortran
subroutine as an array.

my question: how shall i do it? what is morale? what is legal? and what is
practical?

void g()
{
std::vector<Foo> v;
...
f(v.begin(), v.size()); // this is what i cannot do i figure

right: std::vector said:
f(&v[0], v.size()); // this is the yuk-dirty way i came up with

this is fine and blessed by the standard unless Foo is bool.

[snip]


Best

Kai-Uwe Bux
 
L

LR

sebastian sauer wrote:

my case: a C++ std::vector<T> that is also accessed by a fortran subroutine
as an array.

Accessed is a tricky word.

Might you ever call call your fortran code with the data in a
std::vector as a parameter, and then use the fortran to call c++ and, I
don't know, say do something that changes the size of the vector? And
then have the fortran use the values in the array again?

That might be a huge no-no.


my question: how shall i do it?

A way that works.
what is morale?

A way that makes you feel confident.
what is legal?

A way that is approved by the law.
and what is practical?

Following The Way always leads to a practical solution.

Please post follow up to the above questions to a ng that specializes in
practical philosophy. This is the way of comp.lang.c++.



void g()
{
std::vector<Foo> v;
...
f(v.begin(), v.size()); // this is what i cannot do i figure

As has already been pointed out, that won't work.
f(&v[0], v.size()); // this is the yuk-dirty way i came up with

I don't know, and YMMV, but I'd be surprised if the above worked. Perhaps:

int s = v.size();
f(&v[0], &s);

please enlighten me what is here the best thing to do?

1) IMHO, the very best thing, assuming funding, is to rewrite all your
code in one language, and use it. I think c++ might be my choice for
that. Yours might not be.

2) Usually (1) is not very practical, so if I could, I'd put all of the
calls to the fortran code in a wrapper. This may add some inefficiency
to the code, perhaps inline would help. But it will isolate the calls
to fortran and save some problems if you ever want to change either one
of your compilers or your linker, because, and this might be important
to you, if not now, then someday:

Mixed Language Programming Is Not Portable!

For that reason, I'd suggest doing something like (untested):

namespace fortran_stuff { // choose a better name
void f(std::vector<Foo> &v) {
int s = v.size();
::f(&v[0], &s);
}
}

Doing this will also make your c++ code more c++ish looking. And save
some of the aggravation of having to deal with creating a variable in
your code for size everytime you call ::f. Or having a variable that
gets used over and over for that. Because you or someone else will
forget and Oops!

LR
 
S

sebastian sauer

hi,

Friday 14 September 2007 08:47 pm, Kai-Uwe Bux in comp.lang.c++:
sebastian sauer wrote:
right: std::vector said:
f(&v[0], v.size()); // this is the yuk-dirty way i came up with

this is fine and blessed by the standard unless Foo is bool.

i finally found smthg. that works, is elegant, and is similar in look'n'feel
to e.g. boost::multi_array

at least libstdc++ has a data() function member implemented for
std::vector<T>

so now my only question: is this already in the standard, or will it make it
into it?

http://gcc.gnu.org/onlinedocs/libst...1vector.html#6b508c758188019c8019c36b823c5b52
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1589.html
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

hi,

Friday 14 September 2007 08:47 pm, Kai-Uwe Bux in comp.lang.c++:
sebastian sauer wrote:
right: std::vector said:
f(&v[0], v.size()); // this is the yuk-dirty way i came up with

this is fine and blessed by the standard unless Foo is bool.

i finally found smthg. that works, is elegant, and is similar in look'n'feel
to e.g. boost::multi_array

at least libstdc++ has a data() function member implemented for
std::vector<T>

so now my only question: is this already in the standard, or will it make it
into it?

It is not in the current standard but will be in the next.
 

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,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top