c++ - Address of pass-by-value variable causing seg-fault -
i'm working legacy code cannot edit compiled , tested on powerpc. attempting create build system build generic linux box (ubuntu 11.10 x64).
it has custom interface similar cblas wraps f2c version of generic blas library included in clapack (ver. 3.2.1). i.e. compile liblapack, libblas , libf2c on linux machine clapack source , link following example code:
int main() { double a[3] = {100,200,300}; // scale elements of 0.1 // uses custom wrapper seg. faults mycblas_dscal(3,0.1,a,1); } void mycblas_dscal(int n, double scale, double* data, int inc) { dscal_((int*) &n, (double*) &scale, data, (int*) &inc); }
mycblas_dscal
calls blas library implementation dscal_
. library expects pointers data , wrapper passes address of n
,scale
, inc
directly. scares me since passed value , literals.
when executed mycblas_dscal
nothing, i.e. a
unchanged or seg. faults. higher compiler optimizations (e.g. gcc -o3) ever seg. faults.
to test blas library, following code works fine:
int main() { // calls library directly , works fine cannot edit //the rest of code using wrapped version above. int size = 3; double scale = 0.1; int inc = 1; dscal_(&size,&scale,a,&inc); }
my insight address , persistence of pass-by-value variables in wrapper function since seg. fault behavior gets worse compiler optimizations. don't know enough investigate further. ideas?
what function
void mycblas_dscal(int n, double scale, double* data, int inc) { dscal_((int*) &n, (double*) &scale, data, (int*) &inc); }
does declare local variables n
, scale
, inc
, pass addresses on dscal_
. since variables local, live on stack , cease exist once mycblas_dscal
finished. internal dscal_
manipulates these local variables through address. manipulation affects local variables, has no effect on original variables passed mycblas_dscal
. once turn on optimization, guess mycblas_dscal
call inlined, further worsening problem , causing seg faults.
if can change mycblas_dscal
, change follows:
void mycblas_dscal(int &n, double &scale, double*& data, int &inc) { dscal_((int*) &n, (double*) &scale, data, (int*) &inc); }
i.e. use references rather values.
if cannot change definition because it's part of library mentioned, thing can not use library. don't think there other solution in case.
Comments
Post a Comment