help-gsl
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-gsl] gsl_vector_alloc question.


From: Sang Oh
Subject: Re: [Help-gsl] gsl_vector_alloc question.
Date: Wed, 14 Jul 2004 14:29:29 -0600
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4.2) Gecko/20040308


Actually I wanting to pass more than one gsl_vector (or gsl_matrix) to functions to get allocated. I am doing this now. John is right in that it's more a c question than gsl problem. =)

void ruler(double xa, double xb, double dx, gsl_vector **vc)
{
*vc = gsl_vector_alloc(len);
}

main()
{
gsl_vector *v;
ruler(xa,xb,dx,&v);
}

regards,

Sang


John Lamb wrote:

Sang Oh wrote:

I'm trying to allocate memory in a function.

void ruler(double xa, double xb, double dx, gsl_vector *vc) {
 vc = gsl_vector_alloc(len);
}


Looks like a common programming error, nothing to do with gsl. You cannot globally change the _value_ of a parameter. You can only change the value of the struct it points to. The gsl_vector_alloc call just changes a local copy of vc and then throws it away when the function returns, leaving a vector on the heap that you can't point to.

Try rewriting as

gsl_vector *ruler(double xa, double xb, double dx){
  ...
  vc = gsl_vector_alloc(len);
  ...
  return vc;
}







reply via email to

[Prev in Thread] Current Thread [Next in Thread]