Solved

Does this leak memory

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

760 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

20 Experts available now in Live!

Get 1:1 Help Now