Solved

Does this leak memory

Posted on 2013-12-03
3
285 Views
Last Modified: 2013-12-03
struct some_struct {

}

std::vector<some_struct *> list_of_struct;
list_of_struct.push_back(new some_struct());

some_struct* remove_first_member() {
	std::vector<some_struct *>::iterator it( list_of_struct.begin());
	some_struct  *temp = *it;
	 list_of_struct.erase(it);
	return temp;
}

// somewhere in main I am calling
remove_first_member(); // There is no lvalue even though the function returns pointer
                                        // to some_struct

Open in new window


Will this leak memory?
0
Comment
Question by:perlperl
  • 2
3 Comments
 

Author Comment

by:perlperl
ID: 39693528
OR should I do something like this to avoid memory leak?

some_struct* temp = remove_first_member();
delete temp;
0
 

Accepted Solution

by:
perlperl earned 0 total points
ID: 39693550
I got it. I added printf in destructore to verify it. I was leaking memory. I have to delete it as I mentioned in the second comment.

Can the admin please close the Q
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question