LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-07-2003, 11:47 AM   #1
champ
Member
 
Registered: Jul 2002
Distribution: Slackware 10.0
Posts: 46

Rep: Reputation: 16
stl containers


I just have a little question regarding how the stl containers handles memory.

I have allocated memory for a object and then the object is added to an stl list:

Code:
std::list<NewUser> userlist;
......
// adds item to vector
NewUser *user = new NewUser();
userlist.push_back(*user);
.......

// removes item from vector
userlist.pop_front();
I just wondered if the pop_front function frees the memory that I allocated earlier.
If not, how would i do that. The pop_front() function does'nt return the item popped off, does it? If it does I could just run delete on it, but I don't think you can do that?

BTW, the "user" pointer will only point to the current object created, so I will not have a pointer to the objects since this will be run in a function.

The best solution here maybe would not to use a pointer to the object at all, because I don't think I will need them.

But anyway, I would really like to know how it works.

Last edited by champ; 04-07-2003 at 12:58 PM.
 
Old 04-07-2003, 05:34 PM   #2
GtkUser
Member
 
Registered: Sep 2002
Location: Canada
Distribution: Redhat 9.0
Posts: 637

Rep: Reputation: 30
//Allows you to use polymorphism if NewUser is a base class
std::list<NewUser*> userlist;
Yes, you have to free the object manually.

//If you need to use sort on std::list<T*>
bool compare_NewUser_ptrs( const NewUser* cp1, const NewUser * cp2 ) {
return compare( *cp1, *cp2);
}
...
sort(userlist.begin(), userlist.end(), compare_NewUser_ptrs);

I think that one way to be efficient with containers is to simply pass the container by reference as a function argument:
std::list<NewUser> userlist;
NewUser record;
//Fill Record here
userlist.push_back(record);
FunctionCall( userlist );

//Pass list by reference to a function
void FunctionCall( std::list<NewUser> & a , ...);

The containers use iterators to manage the objects they contain. I hear a good book on the subject is Generic Programming and the STL by Austern. The details are over my head but basically use the pointer syntax std::list<T*> and you have to call delete on the elements, but I can't say I've used it more than once in the past, and I'm not sure how all the std::list member functions would work with pointers, but it is legal.
 
Old 04-09-2003, 05:27 AM   #3
champ
Member
 
Registered: Jul 2002
Distribution: Slackware 10.0
Posts: 46

Original Poster
Rep: Reputation: 16
thanks for the tips GtkUser.

One of my problems was that the list contained references instead of pointers.

So now there is no problem iterate through the list and free the memory.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
about STL programming wangjinyi Programming 2 03-24-2005 05:45 PM
Are the QT suuport STL? Barq Linux - General 1 11-18-2004 10:20 AM
how to free linked list containers? rgiggs Programming 3 07-30-2004 01:24 PM
c++ and stl G67 Programming 4 12-17-2003 02:36 AM
C++ STL problem jaycee999 Programming 5 05-31-2002 03:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration