help-gsl
[Top][All Lists]
Advanced

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

Re: [Help-gsl] (no subject)


From: Alejandro Cámara Iglesias
Subject: Re: [Help-gsl] (no subject)
Date: Wed, 24 Aug 2011 21:45:38 +0200

Hello Don,

I am not sure about the segmentation fault you say, because I see several
things that, I think, are not correct.

*(1)* min and max are two pointers that point at *nothing*. I think you
should

   - reserve heap memory to min and max using malloc() (and do not forget to
      use free() after you are done with min or max), or
      - define min and max as variables and then pass a reference to them,
      i.e.

double min, max;  // no pointers
gsl_vector_minmax(v, &min, &max);   // &variable is the
                                    // reference to variable

The second case (passing them as a reference) is what I would use.


*(2)* To change a value in a vector you should use *gsl_vector_set()* instead
of gsl_vector_get(), which is to obtain the value in a position of a vector.
See [1].

*(3)* Remember to deallocate the memory of w after you finished using it via
gsl_vector_free(w). See [2].

I hope this helps you!

Best regards,

[1]:
http://www.gnu.org/software/gsl/manual/html_node/Accessing-vector-elements.html
[2]: http://www.gnu.org/software/gsl/manual/html_node/Vector-allocation.html

*Alejandro Cámara*
PhD Student of the GICO <http://www.ucm.es/info/giboucm/>


2011/8/24 Don <address@hidden>

>
>        Hello! I am a beginner in C programming and I get a problem with
> coding
> while using gsl_vector_minmax(). It's OK for compiling by GCC 4.6.
> However, that will promt "segmentation fault" while executing. In the
> GSL handbook, it suggest that the "const gsl_vector" rather than
> "gsl_vector", I guess the mistake may cause by this point. However, I
> could not construct a "const gsl_vector". In addition, I need some
> suggestions on C/C++ programming, can you give me some advices or web
> sites? I think I need a BBS which focus on GSL~~ :-)
>
>
>        My code is follow:
>
>        #include<stdio.h>
>        #include<gsl/gsl_rng.h>
>        #include<gsl/gsl_randist.h>
>        #include<gsl/gsll_vector.h>
>
>        int main(void)
>        {
>                int i;
>                double *min, *max;
>                gsl_vector *w = gsl_vector_alloc(10);
>                const gsl_rng_type *T;
>                gsl_rng *r;
>
>                gsl_rng_env_setup();
>                T = gsl_rng_default;
>                r = gsl_rng_alloc(T);
>
>                for (i = 0; i < 10; i++)
>                        gsl_vector_get(w, i) = gsl_ran_ugaussian(r);
>
>                gsl_vector_(w, min, max);
>                return 0;
>        }
>
>
> _______________________________________________
> Help-gsl mailing list
> address@hidden
> https://lists.gnu.org/mailman/listinfo/help-gsl
>


reply via email to

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