Solved

Does this leak memory

Posted on 2013-12-03
3
279 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

895 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now