Re: using dot_product from c++
- From: Craig Powers <enigma@xxxxxxxxxx>
- Date: Tue, 12 Feb 2008 13:43:29 -0500
utab wrote:
Dear all,
I tried to write a very simple subroutine where two vector are dot
producted.
Compiles and links fine but, then which a bad_alloc exception in from c
++ side.
Could someone comment on this?
gfortran -c vecpro.f90
g++ -c vecprod.cc
g++ -o test vecpro.o vecprod.o -lg2c
nm vecpro.o
results in
vectorproduct_
and execution results in
./test
terminate called after throwing an instance of 'std::bad_alloc'
what(): St9bad_alloc
Aborted
Codes:
subroutine vectorProduct(res,vec1,vec2,n)
implicit none
integer ::n ! dimension of the
vector
real, dimension(:) ::vec1,vec2 ! dimension of the vectors are
Assumed shape arrays are incompatible with mixed language use unless you code explicitly for the descriptor on the other side, which will be highly nonportable (even from one version of a compiler to another, let alone from one compiler to another).
The correct declaration would be:
REAL, DIMENSION(n) :: vec1, vec2
(as it is, you're not using n at all.)
real res ! result of the
operation
res=dot_product(vec1,vec2) ! dot_product is used to compute the
! result
end subroutine vectorProduct
#include <iostream>
#include <vector>
#include <string>
using namespace std;
extern "C" {
void vectorproduct_(double &,std::vector<double> &,
std::vector<double> &, int &);
}
A Fortran array is *highly* unlikely to be compatible with a C++ std::vector. Furthermore, Fortran parameters are much more likely to be compatible with C/C++ pointers than with C references. The correct prototype would be:
extern "C" void vectorproduct_(double*, double*, double*, int*);
int main(){
int sz;
vector<double> a(5),b(5);
sz=a.size();
int i=1,j=4;
double result;
for(;i!=sz+1;++i){
a.push_back(i);
++i;
}
for(;j!=sz+1;++j){
b.push_back(j);
++j;
}
This code is not doing what I believe you think it's doing. At the end of it, a and b are both length 10, containing;
0 0 0 0 0 1 2 3 4 5
while the value of sz is still 5.
You should either not initialize the vectors (leaving them in the default size of 0) or you should not use push_back but instead reference the location you wish to access directly. (Or, in the third alternative, there may be a standard algorithm that will allow you to perform the initialization on declaration, but a) it will ultimately just reduce to the loop structure, and b) it will be much more opaque to the unfamiliar reader.)
Don't set sz until you're done modifying the size of a and b.
vectorproduct_(result,a,b,sz);
Then, the correct call is:
vectorproduct_(&result, &a[0], &b[0], &sz);
std::cout << result << std::endl;
return 0;
}
I know that vectors are aligned in memort continuously, but could not
figure the problem out, I guess it is sth related to the memory since
I am getting an error related to bad_alloc.
The bad_alloc is a little surprising, I would have expected a simple segfault. I guess maybe it arises because the misunderstandings between the codes multiply to put the memory manager out of whack.
.
- Follow-Ups:
- Re: using dot_product from c++
- From: Gerry Ford
- Re: using dot_product from c++
- From: utab
- Re: using dot_product from c++
- References:
- using dot_product from c++
- From: utab
- using dot_product from c++
- Prev by Date: Re: ISO_C_BINDING and generic interfaces
- Next by Date: Re: using dot_product from c++
- Previous by thread: Re: using dot_product from c++
- Next by thread: Re: using dot_product from c++
- Index(es):
Relevant Pages
|
|