Re: [eigen] API change proposal |
[ Thread Index |
Date Index
| More lists.tuxfamily.org/eigen Archives
]
- To: eigen@xxxxxxxxxxxxxxxxxxx
- Subject: Re: [eigen] API change proposal
- From: Tim Hutt <tdhutt@xxxxxxxxx>
- Date: Fri, 24 Jul 2009 21:40:47 +0100
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :from:date:message-id:subject:to:content-type :content-transfer-encoding; bh=dHkM9Nhu9A36u3CHygrC6YSmh9NukLWKCAsP6ymN8Wg=; b=tEFP8JNBP94DF/QSFvwvz4JlpJR6xqjxJqx+KwUbVYQI9n8yDVQZCevJT5b4XQfvFv r7xlL/QnISk6Byn5lSvywarVJu7XDPoK8FW33zZxbS+60abQsD2J/dkPAw4IjUajcln3 ezxxxR/7nmAdfMA+ZhQp2a7qooraiBWcK9fJs=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; b=mkZHvTXk/EEEB5/LFQNKe/TW9lTILRX3BYcnOHE1FzKXMEjTO9CThUfcnftWgmQRrX FIImrrYmn4VAcCteSZSeACiPqjx+TeNE1K4Tgwmoz3IfqzKl0DGHUfpiIkqYwjpTDvlq q0fmo4a5//uQtN25/0RBcXrVze2fqabiKEsl4=
2009/7/24 Markus Benjamin Fröb <grey_earl@xxxxxx>:
> Matrix<>* LU::solve(const Matrix<>& b) {
> return the solution if it exists, else null
> }
That is a *terrible* API - the sort I'd expect of a C library. Not
only does it force manual memory management, but functions returning
pointers are a very bad idea because it is never documented who should
delete the Matrix<> which leads to memory leaks. Consider all the
mistakes this makes possible:
class Matrix1
{
publc:
Matrix1* solve()
{
cachedSolution = ...;
return &cachedSolution;
}
private:
Matrix1 cachedSolution;
}
class Matrix2
{
public:
Matrix1* solve()
{
Matrix2* solution = new Matrix2;
*solution = ...;
return solution;
}
}
void mistakes(const Matrix1& m1, const Matrix2& m2)
{
Matrix1* lu = m2.solve();
...
if (foo)
return; // lu not freed.
Matrix2* lu2 = m1.solve();
if (foo)
{
delete lu2; // shouldn't have deleted this.
return;
}
} // lu still not freed.