472,964 Members | 2,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,964 software developers and data experts.

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_alloc(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

Mar 31 '07 #1
2 3896
"zl2k" <kd*******@gmail.comwrote in message
news:11*********************@p77g2000hsh.googlegro ups.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_alloc(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.
Mar 31 '07 #2
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_alloc(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_alloc, 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
Mar 31 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Christian Stigen Larsen | last post by:
Consider the following: class parent { public: virtual void print() { printf("Parent\n"); } }; class child : public parent {
27
by: Jason Heyes | last post by:
To my understanding, std::vector does not use reference counting to avoid the overhead of copying and initialisation. Where can I get a reference counted implementation of std::vector? Thanks.
18
by: Janina Kramer | last post by:
hi ng, i'm working on a multiplayer game for a variable number of players and on the client side, i'm using a std::vector<CPlayer> to store informatik about the players. CPlayer is a class that...
11
by: Lynn | last post by:
Hi, Is it better to use std::vector <type> with a pointer to the types being stored or the actual storage for the types ? In other words, is std::vector <mytype> or std::vector <mytype *>...
20
by: Anonymous | last post by:
Is there a non-brute force method of doing this? transform() looked likely but had no predefined function object. std::vector<double> src; std::vector<int> dest; ...
2
by: Priya Mishra | last post by:
Hi All It was very nice to intract with this group, While in my previous post, I was suggested to reffer the link, in order to learn C++, Well I was going thoruigh the link in which i had some...
32
by: zl2k | last post by:
hi, c++ user Suppose I constructed a large array and put it in the std::vector in a function and now I want to return it back to where the function is called. I can do like this: ...
3
by: eb | last post by:
I have in a header file Class MyClasse : public Foo { .... std::vector <int> v; }
13
by: jubelbrus | last post by:
Hi I'm trying to do the following. #include <vector> #include <boost/thread/mutex.hpp> #include <boost/shared_ptr.hpp> #include <boost/tuple/tuple.hpp> class {
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.