[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

memory leak problem

bingo

9/24/2008 5:11:00 PM

Hi, All:
I was really new to C++, so please forgive me if I asked stupid
questions.

I was trying to use this following Vector class(not written by me)
to do some calculations :

=============================================
class Vector {
public:
double x,y,z;

inline Vector(void) : x(-99999), y(-99999), z(-99999) { ; }
// inline Vector(void) { ; }

inline Vector( double newx, double newy, double newz)
: x(newx), y(newy), z(newz) { ; }

inline Vector( double newv ) // allow Vector v = 0; etc.
: x(newv), y(newv), z(newv) { ; }

inline Vector(const FloatVector &v) : x(v.x), y(v.y), z(v.z)
{ ; }

inline double operator[](int i) {
return i==0 ? x
:i==1 ? y
:i==2 ? z
: x;

}

// v1 = v2
inline Vector& operator=(const Vector &v2) {
x = v2.x;
y = v2.y;
z = v2.z;
return *this;
}

// v1 = const;
inline Vector& operator=(const double &v2) {
x = v2;
y = v2;
z = v2;
return *this;
}

// addition of two vectors
inline friend Vector operator+(const Vector& v1, const Vector&
v2) {
return Vector( v1.x+v2.x, v1.y+v2.y, v1.z+v2.z);
}

// negation
inline friend Vector operator-(const Vector &v1) {
return Vector( -v1.x, -v1.y, -v1.z);
}

// subtraction
inline friend Vector operator-(const Vector &v1, const Vector
&v2) {
return Vector( v1.x-v2.x, v1.y-v2.y, v1.z-v2.z);
}

// inner ("dot") product
inline friend double operator*(const Vector &v1, const Vector
&v2) {
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
}
// scalar product
inline friend Vector operator*(const double &f, const Vector &v1)
{
return Vector(f*v1.x, f*v1.y, f*v1.z);
}
// scalar product
inline friend Vector operator*(const Vector &v1, const double &f)
{
return Vector(f*v1.x, f*v1.y, f*v1.z);
}
// division by a scalar
inline friend Vector operator/(const Vector &v1, const double &f)
{
return Vector(v1.x/f, v1.y/f, v1.z/f);
}

};

==========================

My main code looks like this :

int main () {

Vector a, b, c, d;
a = b + c - d;
a = b * 5.0;

}

Now my question is : for the operation - and + and scalar product,
will it return a new Vector and not be released during my calculation,
which finally leads to memory leak? If so, how can I fix this problem?

Thanks a lot.
-Bin




17 Answers

Erik Wikström

9/24/2008 5:52:00 PM

0

On 2008-09-24 19:10, bingo wrote:
> Hi, All:
> I was really new to C++, so please forgive me if I asked stupid
> questions.
>
> I was trying to use this following Vector class(not written by me)
> to do some calculations :
>
> =============================================
> class Vector {
> public:
> double x,y,z;
>
> inline Vector(void) : x(-99999), y(-99999), z(-99999) { ; }
> // inline Vector(void) { ; }
>
> inline Vector( double newx, double newy, double newz)
> : x(newx), y(newy), z(newz) { ; }
>
> inline Vector( double newv ) // allow Vector v = 0; etc.
> : x(newv), y(newv), z(newv) { ; }
>
> inline Vector(const FloatVector &v) : x(v.x), y(v.y), z(v.z)
> { ; }
>
> inline double operator[](int i) {
> return i==0 ? x
> :i==1 ? y
> :i==2 ? z
> : x;
>
> }
>
> // v1 = v2
> inline Vector& operator=(const Vector &v2) {
> x = v2.x;
> y = v2.y;
> z = v2.z;
> return *this;
> }
>
> // v1 = const;
> inline Vector& operator=(const double &v2) {
> x = v2;
> y = v2;
> z = v2;
> return *this;
> }
>
> // addition of two vectors
> inline friend Vector operator+(const Vector& v1, const Vector&
> v2) {
> return Vector( v1.x+v2.x, v1.y+v2.y, v1.z+v2.z);
> }
>
> // negation
> inline friend Vector operator-(const Vector &v1) {
> return Vector( -v1.x, -v1.y, -v1.z);
> }
>
> // subtraction
> inline friend Vector operator-(const Vector &v1, const Vector
> &v2) {
> return Vector( v1.x-v2.x, v1.y-v2.y, v1.z-v2.z);
> }
>
> // inner ("dot") product
> inline friend double operator*(const Vector &v1, const Vector
> &v2) {
> return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
> }
> // scalar product
> inline friend Vector operator*(const double &f, const Vector &v1)
> {
> return Vector(f*v1.x, f*v1.y, f*v1.z);
> }
> // scalar product
> inline friend Vector operator*(const Vector &v1, const double &f)
> {
> return Vector(f*v1.x, f*v1.y, f*v1.z);
> }
> // division by a scalar
> inline friend Vector operator/(const Vector &v1, const double &f)
> {
> return Vector(v1.x/f, v1.y/f, v1.z/f);
> }
>
> };
>
> ==========================
>
> My main code looks like this :
>
> int main () {
>
> Vector a, b, c, d;
> a = b + c - d;
> a = b * 5.0;
>
> }
>
> Now my question is : for the operation - and + and scalar product,
> will it return a new Vector and not be released during my calculation,
> which finally leads to memory leak? If so, how can I fix this problem?

No, the +, -, and scalar product will return Vector-objects, these will
be temporary objects which will die at the end of the expression they
are part of. As a general rule you can not leak memory unless you
allocate it first, which is usually done using 'new', which is not used
in your code.

--
Erik Wikström

bingo

9/24/2008 9:03:00 PM

0

I got it.
Thanks a lot.

sheldonlg

10/1/2013 12:52:00 PM

0

On 30/09/2013 20:57, Giborah wrote:
> Might also have to do with the continuing 5-year recession and the failure of the Mad Dogs in Congress to make the crooks on Wall Street and the bailed-out crook banks loosen up on lending, to create small business, ergo jobs. Not to mention repairing dangerous infrastructure.

Please tell us what you really think about Congress, American business
and the whole banking industry. We really want to know more about how
you feel than your ambivalent statements here.

--
Shelly

sheldonlg

10/1/2013 1:22:00 PM

0

On 01/10/2013 08:51, Shelly wrote:
> On 30/09/2013 20:57, Giborah wrote:
>> Might also have to do with the continuing 5-year recession and the
>> failure of the Mad Dogs in Congress to make the crooks on Wall Street
>> and the bailed-out crook banks loosen up on lending, to create small
>> business, ergo jobs. Not to mention repairing dangerous infrastructure.
>
> Please tell us what you really think about Congress, American business
> and the whole banking industry. We really want to know more about how
> you feel than your ambivalent statements here.
>

Sarcasm aside, I don't know what you are talking about. (BTW, the
recession started 6 months after Bush Jr. took office -- personal
experience from the depression in Massachusetts in July 2001).

My daughter just bought her first house. Prices are still down so she
got a totally renovated house (move-in condition), 3 bedroom, two bath,
den, living room, dining room, central A/C, 1600 square feet on a pretty
half acre and it cost $127,000 with the seller kicking in $4,500 towards
closing costs. It is an FHA loan and so only required about 3% down and
the interest rate was higher because of that (and that rates are rising)
and that it is a 30 year fixed rate note so it was 4.75%. By
comparison, when I bought my first house in 1967 the interest rate was
6.5%. That is one.

Next, my wife and I each opened up (no charge) checking accounts at
Chase [Morgan] Bank after receiving offers in the mail of $150 each if
we did so. Seemed like a sweet deal to me.

I opened a new credit card at Chase because they offered 40,000 points
for doing so. Points are redeemable for flights, vacations, etc. or for
cash at a penny a point. That equates to $400 just for opening it up,
let alone that I put about $25,000 annually on it so that is another
$250 coming bank to me, thank you Chase. (Everything goes on it and I
pay the entire balance each month).

I had refinanced my home two years ago dropping my rate from 7.5% (in
2006) to 3.75%. Earlier this year THEY called ME and said I could
refinance AT NO COST TO ME at 3.25% -- so I did. The documentation
required was thorough -- more so than a decade ago, but so what? Isn't
that a GOOD thing?

So, credit crunch? What credit crunch? THEY came after ME to lend me
money at GREAT rates and benefits.

--
Shelly

Higgs Boson

10/1/2013 6:02:00 PM

0

On Tuesday, October 1, 2013 6:21:52 AM UTC-7, shel...@thevillages.net wrote:
> On 01/10/2013 08:51, Shelly wrote:
>
> > On 30/09/2013 20:57, Giborah wrote:
>
> >> Might also have to do with the continuing 5-year recession and the
>
> >> failure of the Mad Dogs in Congress to make the crooks on Wall Street
>
> >> and the bailed-out crook banks loosen up on lending, to create small
>
> >> business, ergo jobs. Not to mention repairing dangerous infrastructure.
>
> >
>
> > Please tell us what you really think about Congress, American business
>
> > and the whole banking industry. We really want to know more about how
>
> > you feel than your ambivalent statements here.
>
> >
>
>
>
> Sarcasm aside, I don't know what you are talking about. (BTW, the
>
> recession started 6 months after Bush Jr. took office -- personal
>
> experience from the depression in Massachusetts in July 2001).
>
>
>
> My daughter just bought her first house. Prices are still down so she
>
> got a totally renovated house (move-in condition), 3 bedroom, two bath,
>
> den, living room, dining room, central A/C, 1600 square feet on a pretty
>
> half acre and it cost $127,000 with the seller kicking in $4,500 towards
>
> closing costs. It is an FHA loan and so only required about 3% down and
>
> the interest rate was higher because of that (and that rates are rising)
>
> and that it is a 30 year fixed rate note so it was 4.75%. By
>
> comparison, when I bought my first house in 1967 the interest rate was
>
> 6.5%. That is one.
>
>
>
> Next, my wife and I each opened up (no charge) checking accounts at
>
> Chase [Morgan] Bank after receiving offers in the mail of $150 each if
>
> we did so. Seemed like a sweet deal to me.
>
>
>
> I opened a new credit card at Chase because they offered 40,000 points
>
> for doing so. Points are redeemable for flights, vacations, etc. or for
>
> cash at a penny a point. That equates to $400 just for opening it up,
>
> let alone that I put about $25,000 annually on it so that is another
>
> $250 coming bank to me, thank you Chase. (Everything goes on it and I
>
> pay the entire balance each month).
>
>
>
> I had refinanced my home two years ago dropping my rate from 7.5% (in
>
> 2006) to 3.75%. Earlier this year THEY called ME and said I could
>
> refinance AT NO COST TO ME at 3.25% -- so I did. The documentation
>
> required was thorough -- more so than a decade ago, but so what? Isn't
>
> that a GOOD thing?
>
>
>
> So, credit crunch? What credit crunch? THEY came after ME to lend me
>
> money at GREAT rates and benefits.
>

I really shouldn't waste electrons on a reply. Shel, continue blissfully safe in your privileged class; never mind that your countrymen (maybe even including some fellow Jews?) are being ****ed through no fault of their own.

(And don't bother with the mortgage thing; it was carefully engineered to take advantage of (a) the most ignorant and gullible and (b) the most greedy who never bothered to study history of past boom&bust. Absolute criminal conduct; visible to even the most privileged viewer when peon-perps confess on TV.)

Nobody is in jail. They're in bed together.

Giborah

Yisroel Markov

10/1/2013 6:09:00 PM

0

On Tue, 1 Oct 2013 13:21:52 +0000 (UTC), Shelly
<sheldonlg@thevillages.net> said:

>On 01/10/2013 08:51, Shelly wrote:
>> On 30/09/2013 20:57, Giborah wrote:
>>> Might also have to do with the continuing 5-year recession and the
>>> failure of the Mad Dogs in Congress to make the crooks on Wall Street
>>> and the bailed-out crook banks loosen up on lending, to create small
>>> business, ergo jobs. Not to mention repairing dangerous infrastructure.
>>
>> Please tell us what you really think about Congress, American business
>> and the whole banking industry. We really want to know more about how
>> you feel than your ambivalent statements here.
>>
>
>Sarcasm aside, I don't know what you are talking about. (BTW, the
>recession started 6 months after Bush Jr. took office -- personal
>experience from the depression in Massachusetts in July 2001).

Shelly, personal experience is not data.

Your account below suffers from the same problem. You present your
personal experience - that of a fairly affluent person with most
likely a stellar credit score - and that of your daughter, who got an
FHA loan (and FHA is likely another disaster waiting to happen, BTW).
IMHO it's not prudent to extrapolate that to the entire country.

I have access to data. What I see is that consumer lending to people
like you and me has not only recovered - it had never decreased
meaningfully even in the depths of the crisis. (Personal anecdote - I
got two mortgages with no problems in September 2008.) But move down
the score ladder and/or up the loan-to-value ratio, and the situation
is very different. There is, of course, a strong case to be made that
it *should* be different - the substance of the crisis was reckless
lending. But the hangover is there.

(Incidentally, this is why I don't blame the Obama administration for
the tepidness of this jobless recovery. While it's done some damage, I
don't see how it would've been very different otherwise. A lot of the
prosperity of the previous decade was borrowed; those levels of
consumer spending were unsustainable, that much was clear even back in
2005. No stimulus can fix that - the debt levels have to come down, or
at least stop growing.)

Business lending has recovered, with a vengeance, on the high end. If
you're a large company with a solid balance sheet, bank and non-bank
lenders alike are tripping over their own feet in the rush to lend you
money "at great rates and benefits." But much of it is refinancing
rather than for new projects, and again, lower on the food chain the
situation is different. There, the government does get some blame -
its policies have long encouraged bank consolidation (similar to how
they did medical consolidation), and the post-crisis policies have
tied up what smaller banks remain. The tax and especially regulatory
uncertainty of the past few years have strongly demotivated small and
midsize businesses from risking new projects, so the community banks
suffer from both lack of qualified credit demand *and* fear of lending
to less-than-A-level companies. So they buy T-bills instead.

[snip]
--
Yisroel "Godwrestler Warriorson" Markov - Boston, MA Member
www.reason.com -- for a sober analysis of the world DNRC
--------------------------------------------------------------------
"Judge, and be prepared to be judged" -- Ayn Rand

Higgs Boson

10/1/2013 7:09:00 PM

0

On Tuesday, October 1, 2013 5:51:49 AM UTC-7, shel...@thevillages.net wrote:
> On 30/09/2013 20:57, Giborah wrote:
>
> > Might also have to do with the continuing 5-year recession and the failure of the Mad Dogs in Congress to make the crooks on Wall Street and the bailed-out crook banks loosen up on lending, to create small business, ergo jobs. Not to mention repairing dangerous infrastructure.
>
>
>
> Please tell us what you really think about Congress, American business
>
> and the whole banking industry. We really want to know more about how
>
> you feel than your ambivalent statements here.
>
>
Look up "ambivalent".

Giborah-

sheldonlg

10/1/2013 7:50:00 PM

0

On 01/10/2013 15:08, Giborah wrote:
> On Tuesday, October 1, 2013 5:51:49 AM UTC-7, shel...@thevillages.net wrote:
>> On 30/09/2013 20:57, Giborah wrote:
>>
>>> Might also have to do with the continuing 5-year recession and the failure of the Mad Dogs in Congress to make the crooks on Wall Street and the bailed-out crook banks loosen up on lending, to create small business, ergo jobs. Not to mention repairing dangerous infrastructure.
>>
>>
>> Please tell us what you really think about Congress, American business
>>
>> and the whole banking industry. We really want to know more about how
>>
>> you feel than your ambivalent statements here.
>>
>>
> Look up "ambivalent".
>
> Giborah-

I know what ambivalent means. Look up "sarcasm".

--
Shelly

sheldonlg

10/1/2013 8:34:00 PM

0

On 01/10/2013 14:01, Giborah wrote:
> On Tuesday, October 1, 2013 6:21:52 AM UTC-7, shel...@thevillages.net wrote:
>> On 01/10/2013 08:51, Shelly wrote:
>>
>>> On 30/09/2013 20:57, Giborah wrote:
>>>> Might also have to do with the continuing 5-year recession and the
>>>> failure of the Mad Dogs in Congress to make the crooks on Wall Street
>>>> and the bailed-out crook banks loosen up on lending, to create small
>>>> business, ergo jobs. Not to mention repairing dangerous infrastructure.
>>> Please tell us what you really think about Congress, American business
>>> and the whole banking industry. We really want to know more about how
>>> you feel than your ambivalent statements here.
>>
>>
>> Sarcasm aside, I don't know what you are talking about. (BTW, the
>>
>> recession started 6 months after Bush Jr. took office -- personal
>>
>> experience from the depression in Massachusetts in July 2001).
>>
>>
>>
>> My daughter just bought her first house. Prices are still down so she
>>
>> got a totally renovated house (move-in condition), 3 bedroom, two bath,
>>
>> den, living room, dining room, central A/C, 1600 square feet on a pretty
>>
>> half acre and it cost $127,000 with the seller kicking in $4,500 towards
>>
>> closing costs. It is an FHA loan and so only required about 3% down and
>>
>> the interest rate was higher because of that (and that rates are rising)
>>
>> and that it is a 30 year fixed rate note so it was 4.75%. By
>>
>> comparison, when I bought my first house in 1967 the interest rate was
>>
>> 6.5%. That is one.
>>
>>
>>
>> Next, my wife and I each opened up (no charge) checking accounts at
>>
>> Chase [Morgan] Bank after receiving offers in the mail of $150 each if
>>
>> we did so. Seemed like a sweet deal to me.
>>
>>
>>
>> I opened a new credit card at Chase because they offered 40,000 points
>>
>> for doing so. Points are redeemable for flights, vacations, etc. or for
>>
>> cash at a penny a point. That equates to $400 just for opening it up,
>>
>> let alone that I put about $25,000 annually on it so that is another
>>
>> $250 coming bank to me, thank you Chase. (Everything goes on it and I
>>
>> pay the entire balance each month).
>>
>>
>>
>> I had refinanced my home two years ago dropping my rate from 7.5% (in
>>
>> 2006) to 3.75%. Earlier this year THEY called ME and said I could
>>
>> refinance AT NO COST TO ME at 3.25% -- so I did. The documentation
>>
>> required was thorough -- more so than a decade ago, but so what? Isn't
>>
>> that a GOOD thing?
>>
>>
>>
>> So, credit crunch? What credit crunch? THEY came after ME to lend me
>>
>> money at GREAT rates and benefits.
>>
> I really shouldn't waste electrons on a reply. Shel, continue blissfully safe in your privileged class;

Me in the "privileged class"? Hardly. I am somewhere between middle
middle class and upper middle class after working my whole life and
getting out of abject poverty in my youth. Reminder: I grew up on the
lower east side of NY - east side kids/bowery boys area. It was a
ghetto/slum.

> never mind that your countrymen (maybe even including some fellow Jews?) are being ****ed through no fault of their own.

If they are, that is somehow my fault/responsibility? Strange!

>
> (And don't bother with the mortgage thing; it was carefully engineered to take advantage of (a) the most ignorant and gullible

Huh? I have absolutely NO idea what you are talking about. Mortgages
are bad? Go try to buy a house if you can't get a mortgage -- unless
you are one of the "privileged class" who has that kind of loose
change. I don't.

> and (b) the most greedy who never bothered to study history of past boom&bust.

Again I have NO idea what you are talking about.

> Absolute criminal conduct; visible to even the most privileged viewer when peon-perps confess on TV.)

Third time I have NO idea what you are talking about.

>
> Nobody is in jail. They're in bed together.

Who is in bed with whom?

--
Shelly

DoD

10/1/2013 10:10:00 PM

0



"Shelly" <sheldonlg@thevillages.net> wrote in message
news:l2f856$e80$1@dont-email.me...
> On 01/10/2013 14:01, Giborah wrote:

>> I really shouldn't waste electrons on a reply. Shel, continue blissfully
>> safe in your privileged class;

Excuse me? That is MY nickname for Mr. Glickler, thank you very much.

> Me in the "privileged class"? Hardly. I am somewhere between middle
> middle class and upper middle class after working my whole life and
> getting out of abject poverty in my youth. Reminder: I grew up on the
> lower east side of NY - east side kids/bowery boys area. It was a
> ghetto/slum.

Do give us that. We know you are up there in your ivory tower, looking down
and thumbing your nose at us little people.