markzzz
markzzz

Reputation: 47945

Would this "on-fly" passing object suffer of memory leak?

I'm creating (allocating) "on-fly" a complex number while passing it to an exp() function. Whole code:

std::complex<double> resZeros(0.0, 0.0);
resZeros = a0 * std::exp(std::complex<double>(0.0, -0 * freq * 2 * M_PI));

Will this introduce memory leak? In fact I don't manually destroy std::complex<double>(0.0, -0 * freq * 2 * M_PI).

Or is std::complex smart enough to delete it when its out of scope?

Upvotes: 2

Views: 39

Answers (1)

Bathsheba
Bathsheba

Reputation: 234715

No that's fine: std::complex<double>(0.0, -0 * freq * 2 * M_PI) is an anonymous temporary and the C++ standard is very specific in saying that it "lives" for as long as the statement; conceptually the destructor to std::complex is called just after the assignment to resZeros.

Anonymous temporaries do not cause memory leaks.

Upvotes: 2

Related Questions