Tuesday, 27 September 2016

Can this c++ vector initialization cause memory leak?

I have the following code:



#include 
#include

class Test
{
public:
int first;
int second;


Test(int a, int b)
{
first = a;
second = b;
}
};

int main(int argc, char* argv[])
{

std::vector mydata({ Test(4, 8), Test(5, 3), Test(12, 7), Test(8, 9) });

for (auto const&y : mydata)
{
std::cout << y.first << " / " << y.second << std::endl;
}

//need to free vector here or sth?

return 0;

}


I use class constructor to initialize vector.
Is the code above good or can cause errors?
Should I free the vector at the end of the program?

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...