how to release memory allocated

hi,
the below program is working fine but it has multiple memory leaks , please help from where should i release the memories allocated.note- memory is allocated multiple times for storing values fetched from input file, i don't want to use smart pointer as of now.
input - I am reading from a file named- test.txt ,contents are
hello world
beautiful world

program is -

#include <sstream>
#include <vector>
#include <iostream>
#include <fstream>
using namespace std;
struct profile
{
string fname;
string lname;
};

int main()
{

vector< vector<profile*> >vec1;
vector<profile*>vec2;
string line;
ifstream infile("test.txt");
while(getline(infile,line)){
profile *p=NULL;
istringstream ss(line);
if(ss)
{
p=new profile();
ss>>p->fname>>p->lname;
// cout<<"fname= "<<p.fname<<endl;
vec2.push_back(p);
}
vec1.push_back(vec2);
//delete p; //here i am getting error if enable this line
}//while loop
for(int i=0;i<vec2.size();i++)
{
cout<<"fname= "<<vec2[i]->fname<<" "<<"lname= "<<vec2[i]->lname<<endl;

}
return 0;

}


OUTPUT-
fname= hello lname= world
fname= beautiful lname= world
Why do you use pointers at all? You can easily do it with normal profile variables.
hi , similar code is in my project ,i can't change that but yes it will be good learning for me also if i know the pros and cons,i know other solutions are possible but what if i want to de allocate the memory occupied.
it will be good learning for me also if i know the pros

There are no advantages to storing pointers in this particular case.

The code below shows how to free the memory, but it is unequivocally awful. Deterministic resource management is a core strength of the language but the user code below ignores all the features which make it easy.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <sstream>
#include <vector>
#include <iostream>
#include <fstream>

struct profile {
    std::string fname; 
    std::string lname; 
};

namespace {
    // free the memory owned by v
    void release(std::vector<profile*>& v) noexcept
    { for (auto const& elt: v) delete elt; v.clear(); }
}

// may throw
profile* make_new_profile(std::string line) { 
    std::istringstream ss{line};
    
    profile p; 
    if (ss >> p.fname >> p.lname) {
        return new profile{std::move(p)}; 
    } else return nullptr;
}

int main()
{
    // don't know what this is
    // std::vector<std::vector<profile*>> vec1;
    std::vector<profile*>              vec2;
    std::ifstream infile("test.txt");
    
    for (std::string line; std::getline(infile, line); ) {
        profile* pp = nullptr;
        try {
            // pp takes ownership of the new profile
            pp = make_new_profile(line); 
            
            // std::vector::push_back provides a strong exception guarantee
            // the object will not be changed if the operation throws.
            // vec2 takes ownership of *pp
            if (pp) vec2.push_back(pp); 
        } catch (std::exception& e) { 
            std::cerr << e.what(); 
            // release memory in reverse order
            // free the memory owned by pp
            delete pp;
            // free the memory currently owned by vec2
            release(vec2);  
            
            return 1;
        }
    }
    
    for(auto const& elt: vec2) {
        std::cout << "fname = " << elt->fname << " " 
                  << "lname = " << elt->lname << '\n';
    }
    
    release(vec2);
    return 0;
}

http://coliru.stacked-crooked.com/a/782f77b07fdd1a33
A superior version:
http://coliru.stacked-crooked.com/a/dcb51afa9c48d349
Last edited on
@mbozzi (907) highly appreciate your efforts.
Topic archived. No new replies allowed.