std::vector question related with gsl_vector

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • zl2k

    #1

    std::vector question related with gsl_vector

    hi, all

    I need to use gsl_vector pointer with std::vector but not sure how to
    free the memory when I don't need it. Here is a piece of the code.

    =============== ====
    std::vector<gsl _vector * v;
    gsl_vector * a = gsl_vector_allo c(3);
    gsLvector_set(a , 0, 7);
    v.push_back(a);

    gsl_vector_free (a); // problem! after a is free, the v[0] is null
    std::cout<<gsl_ vector_get(v[0], 0)<<std::endl;
    =============== ====

    How can I free the memory when I don't need them? If I say

    v.clear();

    will the memory allocated by the gsl_vector still hold by the
    unreachable vectors?
    Or should I do

    for (int i = 0; i < v.size(); i++){
    gsl_vector_free (v[i]);
    }
    v.clear();

    to free the memory? But iterating on each elements of the vector is
    tedious if the std::vector is long. Is there any easy way to deal with
    the problem? Thanks for comments.

    zl2k

  • Jim Langston

    #2
    Re: std::vector question related with gsl_vector

    "zl2k" <kdsfinger@gmai l.comwrote in message
    news:1175320118 .302871.39980@p 77g2000hsh.goog legroups.com...
    hi, all
    >
    I need to use gsl_vector pointer with std::vector but not sure how to
    free the memory when I don't need it. Here is a piece of the code.
    >
    =============== ====
    std::vector<gsl _vector * v;
    gsl_vector * a = gsl_vector_allo c(3);
    gsLvector_set(a , 0, 7);
    v.push_back(a);
    >
    gsl_vector_free (a); // problem! after a is free, the v[0] is null
    std::cout<<gsl_ vector_get(v[0], 0)<<std::endl;
    =============== ====
    >
    How can I free the memory when I don't need them? If I say
    >
    v.clear();
    >
    will the memory allocated by the gsl_vector still hold by the
    unreachable vectors?
    Or should I do
    >
    for (int i = 0; i < v.size(); i++){
    gsl_vector_free (v[i]);
    }
    v.clear();
    >
    to free the memory? But iterating on each elements of the vector is
    tedious if the std::vector is long. Is there any easy way to deal with
    the problem? Thanks for comments.
    You only need a pointer to the instance to use delete. Deleting the pointer
    itself does not delete the instance. Some would advise smart pointer here.

    All you have to remember is, you used new once, use delete once.

    Lets show a simpler example.

    int* a = new int;
    int* b = a;

    delete b;

    Now both a and b point to invalid memory.

    int * a = new int;
    int* b = a;
    a = NULL;
    delete b;

    We can still delete the memory since we have a pointer to it. Changing one
    of the pointers itself does not change the fact the memory was allocated
    with new and we need to call delete on it.

    So, specific to your question, do not call
    gsl_vector_free (a);
    until you are done with your vector. You have 2 pointers pointing to the
    same memory, a and an element in your std::vector. Go ahead and ignore a,
    reuse it, whatever. It now longer "owns" the instance.

    So, yes, after you are done with the vector, iterate through the vector and
    delete (in yoru cass gsl_vector_free ) the memory.

    malloc and free are the same as new and delete, you call maloc/new once you
    call free/delete once.


    Comment

    • Mark P

      #3
      Re: std::vector question related with gsl_vector

      zl2k wrote:
      hi, all
      >
      I need to use gsl_vector pointer with std::vector but not sure how to
      free the memory when I don't need it. Here is a piece of the code.
      gsl_vector is not standard C++ which makes it outside the scope of this
      group.
      >
      =============== ====
      std::vector<gsl _vector * v;
      gsl_vector * a = gsl_vector_allo c(3);
      gsLvector_set(a , 0, 7);
      v.push_back(a);
      >
      gsl_vector_free (a); // problem! after a is free, the v[0] is null
      Not likely. v holds a pointer value and whatever gsl_vector_free does,
      I seriously doubt that it's going to change the *contents* of vector v.
      More likely the pointer value stored in v[0] is invalid and should not
      be dereferenced, but that's not the same as being NULL.
      std::cout<<gsl_ vector_get(v[0], 0)<<std::endl;
      =============== ====
      >
      How can I free the memory when I don't need them? If I say
      >
      v.clear();
      >
      will the memory allocated by the gsl_vector still hold by the
      unreachable vectors?
      v.clear() will empty out the vector v. If that vector holds pointers,
      it will do _nothing_ insofar as the objects pointed to by those pointers
      are concerned.
      Or should I do
      >
      for (int i = 0; i < v.size(); i++){
      gsl_vector_free (v[i]);
      }
      Not knowing anything about the gsl classes I can't be sure, but if
      that's the proper way to deallocate the resource obtained by
      gsl_vector_allo c, then yes, this is probably what you need to do.
      v.clear();
      This is not necessary in general. The vector will clear itself when it
      is destructed. Only if you want to clear v sometime before it goes out
      of scope do you need to do so explicitly.
      >
      to free the memory? But iterating on each elements of the vector is
      tedious if the std::vector is long.
      Why is it tedious? It looks like two lines of code and, as far as I can
      tell, it takes at least as much work to create them in the first place.

      Of course there are good reasons not to do this iteration, namely that
      it's probably not exception safe and in any event is an additional
      burden upon the programmer. A more robust approach would be to store
      some sort of smart pointer to gsl_vector in your std::vector.

      Is there any easy way to deal with
      the problem? Thanks for comments.
      >
      zl2k
      >

      Comment

      Working...