Connecting Tech Pros Worldwide Forums | Help | Site Map

problem passing value

dave
Guest
 
Posts: n/a
#1: Oct 11 '05
void CalcPortGrossRet(Funds tf[],int fsize,PortFolio tp,int cmonth)
{
int i=0;
float totgrossdlrval=0;
char converter[10];
while(i<fsize){
tf[i].enddlrval=(tf[i].dlrval*(tf[i].ret[cmonth]/100.0)+tf[i].dlrval);
totgrossdlrval+=tf[i].enddlrval;
i++;
}
tp.grossenddlrval=totgrossdlrval;
tp.ret[cmonth]=(tp.grossenddlrval-tp.grossdlrval)/tp.grossdlrval;
sprintf(converter,"%.5f",tp.ret[cmonth]);
MessageBox(NULL,"Monthly return for portfolio is "
+ (CString)converter,"C++ Debugger",NULL);

}

after CalcPortGrossRet is called with
CalcPortGrossRet(thefunds,3,theport,im);
and returns,
theport.grossenddlrval has garbage in it.

While in function tp.grossenddlrval is correct.
tp.grossenddlrval is a float.
theport is defined in the calling function.
whats happening?

thanks




Dan Cernat
Guest
 
Posts: n/a
#2: Oct 11 '05

re: problem passing value



dave wrote:[color=blue]
> void CalcPortGrossRet(Funds tf[],int fsize,PortFolio tp,int cmonth)
> {
> int i=0;
> float totgrossdlrval=0;
> char converter[10];
> while(i<fsize){
> tf[i].enddlrval=(tf[i].dlrval*(tf[i].ret[cmonth]/100.0)+tf[i].dlrval);
> totgrossdlrval+=tf[i].enddlrval;
> i++;
> }
> tp.grossenddlrval=totgrossdlrval;
> tp.ret[cmonth]=(tp.grossenddlrval-tp.grossdlrval)/tp.grossdlrval;
> sprintf(converter,"%.5f",tp.ret[cmonth]);
> MessageBox(NULL,"Monthly return for portfolio is "
> + (CString)converter,"C++ Debugger",NULL);
>
> }
>
> after CalcPortGrossRet is called with
> CalcPortGrossRet(thefunds,3,theport,im);
> and returns,
> theport.grossenddlrval has garbage in it.
>
> While in function tp.grossenddlrval is correct.
> tp.grossenddlrval is a float.
> theport is defined in the calling function.
> whats happening?
>
> thanks[/color]

tp is an object. Does it have the proper copy constructor?

/dan

Alf P. Steinbach
Guest
 
Posts: n/a
#3: Oct 11 '05

re: problem passing value


* dave:[color=blue]
> void CalcPortGrossRet(Funds tf[],int fsize,PortFolio tp,int cmonth)
> {
> int i=0;
> float totgrossdlrval=0;
> char converter[10];
> while(i<fsize){
> tf[i].enddlrval=(tf[i].dlrval*(tf[i].ret[cmonth]/100.0)+tf[i].dlrval);[/color]

Possible buffer overrun.
[color=blue]
> totgrossdlrval+=tf[i].enddlrval;
> i++;
> }
> tp.grossenddlrval=totgrossdlrval;[/color]

'tp' is passed by value into this function, this should have no effect.

[color=blue]
> tp.ret[cmonth]=(tp.grossenddlrval-tp.grossdlrval)/tp.grossdlrval;[/color]

Possible buffer overrun.

In addition, for this to have any effect when there's no buffer overrun,
'tp.ret' would need to be a pointer.

Also, it's rather gross using public members...

[color=blue]
> sprintf(converter,"%.5f",tp.ret[cmonth]);[/color]

Possible buffer overrun.

[color=blue]
> MessageBox(NULL,"Monthly return for portfolio is "
> + (CString)converter,"C++ Debugger",NULL);
>
> }[/color]

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Dan Cernat
Guest
 
Posts: n/a
#4: Oct 11 '05

re: problem passing value



don't toppost.


dave wrote:[color=blue]
> I think I must pass as a reference to change values, ret changed because ret
> is defined as an array of floats so...
> it is automatically passed by reference.
> I thought classes where always passed by reference?[/color]
C++ is not Java or C#[color=blue]
> Is this true ?[/color]
no, it is not true.
[color=blue]
> I'm a little confused.
> Is this true ?[/color]
[snip]

this makes me ask again: does your class has a proper copy constructor?

or pass by reference.

/dan

dave
Guest
 
Posts: n/a
#5: Oct 11 '05

re: problem passing value


I think I must pass as a reference to change values, ret changed because ret
is defined as an array of floats so...
it is automatically passed by reference.
I thought classes where always passed by reference?
Is this true ?
I'm a little confused.
Is this true ?

thanks

"dave" <spammer@hotmail.com> wrote in message
news:iuU2f.10963$Lp.5411@bignews5.bellsouth.net...[color=blue]
> void CalcPortGrossRet(Funds tf[],int fsize,PortFolio tp,int cmonth)
> {
> int i=0;
> float totgrossdlrval=0;
> char converter[10];
> while(i<fsize){
> tf[i].enddlrval=(tf[i].dlrval*(tf[i].ret[cmonth]/100.0)+tf[i].dlrval);
> totgrossdlrval+=tf[i].enddlrval;
> i++;
> }
> tp.grossenddlrval=totgrossdlrval;
> tp.ret[cmonth]=(tp.grossenddlrval-tp.grossdlrval)/tp.grossdlrval;
> sprintf(converter,"%.5f",tp.ret[cmonth]);
> MessageBox(NULL,"Monthly return for portfolio is "
> + (CString)converter,"C++ Debugger",NULL);
>
> }
>
> after CalcPortGrossRet is called with
> CalcPortGrossRet(thefunds,3,theport,im);
> and returns,
> theport.grossenddlrval has garbage in it.
>
> While in function tp.grossenddlrval is correct.
> tp.grossenddlrval is a float.
> theport is defined in the calling function.
> whats happening?
>
> thanks
>
>
>[/color]


AnonMail2005@gmail.com
Guest
 
Posts: n/a
#6: Oct 11 '05

re: problem passing value


Instances of classes and built in types are always passed by value.
Arrays, of course, are passed as a pointer to the first element of the
array. If you want to pass by reference you need to explicitly say so.

dave
Guest
 
Posts: n/a
#7: Oct 11 '05

re: problem passing value



"Dan Cernat" <dcernat@excite.com> wrote in message
news:1129060004.723569.159570@f14g2000cwb.googlegr oups.com...[color=blue]
>
> dave wrote:[color=green]
> > void CalcPortGrossRet(Funds tf[],int fsize,PortFolio tp,int cmonth)
> > {
> > int i=0;
> > float totgrossdlrval=0;
> > char converter[10];
> > while(i<fsize){
> > tf[i].enddlrval=(tf[i].dlrval*(tf[i].ret[cmonth]/100.0)+tf[i].dlrval);
> > totgrossdlrval+=tf[i].enddlrval;
> > i++;
> > }
> > tp.grossenddlrval=totgrossdlrval;
> > tp.ret[cmonth]=(tp.grossenddlrval-tp.grossdlrval)/tp.grossdlrval;
> > sprintf(converter,"%.5f",tp.ret[cmonth]);
> > MessageBox(NULL,"Monthly return for portfolio is "
> > + (CString)converter,"C++ Debugger",NULL);
> >
> > }
> >
> > after CalcPortGrossRet is called with
> > CalcPortGrossRet(thefunds,3,theport,im);
> > and returns,
> > theport.grossenddlrval has garbage in it.
> >
> > While in function tp.grossenddlrval is correct.
> > tp.grossenddlrval is a float.
> > theport is defined in the calling function.
> > whats happening?
> >
> > thanks[/color]
>
> tp is an object. Does it have the proper copy constructor?
>
> /dan
>[/color]


no copy constructor isnt't there a c++ defualt?


Dan Cernat
Guest
 
Posts: n/a
#8: Oct 11 '05

re: problem passing value



dave wrote:[color=blue]
> "Dan Cernat" <dcernat@excite.com> wrote in message
> news:1129060004.723569.159570@f14g2000cwb.googlegr oups.com...[color=green]
> >
> > dave wrote:[color=darkred]
> > > void CalcPortGrossRet(Funds tf[],int fsize,PortFolio tp,int cmonth)
> > > {
> > > int i=0;
> > > float totgrossdlrval=0;
> > > char converter[10];
> > > while(i<fsize){
> > > tf[i].enddlrval=(tf[i].dlrval*(tf[i].ret[cmonth]/100.0)+tf[i].dlrval);
> > > totgrossdlrval+=tf[i].enddlrval;
> > > i++;
> > > }
> > > tp.grossenddlrval=totgrossdlrval;
> > > tp.ret[cmonth]=(tp.grossenddlrval-tp.grossdlrval)/tp.grossdlrval;
> > > sprintf(converter,"%.5f",tp.ret[cmonth]);
> > > MessageBox(NULL,"Monthly return for portfolio is "
> > > + (CString)converter,"C++ Debugger",NULL);
> > >
> > > }
> > >
> > > after CalcPortGrossRet is called with
> > > CalcPortGrossRet(thefunds,3,theport,im);
> > > and returns,
> > > theport.grossenddlrval has garbage in it.
> > >
> > > While in function tp.grossenddlrval is correct.
> > > tp.grossenddlrval is a float.
> > > theport is defined in the calling function.
> > > whats happening?
> > >
> > > thanks[/color]
> >
> > tp is an object. Does it have the proper copy constructor?
> >
> > /dan
> >[/color]
>
>
> no copy constructor isnt't there a c++ defualt?[/color]

if you don't supply a copy constructor, the compiler will generate one
for you. That doesnt mean that the compiler-generated copy constructor
always does the right thing. it just copies member by member the
original object. If one of the members is a pointer, it will copy the
pointer so you will and up with two objects pointing to the same memory
area. check it out.

however, post the minimal compilable amount of code that shows your
problem if you want more.

/dan

Greg Comeau
Guest
 
Posts: n/a
#9: Oct 11 '05

re: problem passing value


In article <1129060929.454735.169600@g47g2000cwa.googlegroups .com>,
<AnonMail2005@gmail.com> wrote:[color=blue]
>Instances of classes and built in types are always passed by value.
>Arrays, of course, are passed as a pointer to the first element of the
>array. If you want to pass by reference you need to explicitly say so.[/color]

<nitpick on>
Meaning that for your first sentence to be true, the last one can't be :)
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
dave
Guest
 
Posts: n/a
#10: Oct 12 '05

re: problem passing value


Why would a copy constructor cause the behavior I require, in this situation
isn't
passing by ref may only option?
"Dan Cernat" <dcernat@excite.com> wrote in message
news:1129060847.375092.105330@o13g2000cwo.googlegr oups.com...[color=blue]
>
> don't toppost.
>
>
> dave wrote:[color=green]
> > I think I must pass as a reference to change values, ret changed because[/color][/color]
ret[color=blue][color=green]
> > is defined as an array of floats so...
> > it is automatically passed by reference.
> > I thought classes where always passed by reference?[/color]
> C++ is not Java or C#[color=green]
> > Is this true ?[/color]
> no, it is not true.
>[color=green]
> > I'm a little confused.
> > Is this true ?[/color]
> [snip]
>
> this makes me ask again: does your class has a proper copy constructor?
>
> or pass by reference.
>
> /dan
>[/color]


Alf P. Steinbach
Guest
 
Posts: n/a
#11: Oct 12 '05

re: problem passing value


* dave:[color=blue]
> Why would a copy constructor cause the behavior I require, in this situation
> isn't
> passing by ref may only option?
> "Dan Cernat" <dcernat@excite.com> wrote in message
> news:1129060847.375092.105330@o13g2000cwo.googlegr oups.com...[color=green]
> >
> > don't toppost.[/color][/color]

What part of "don't toppost" did you not understand?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
dave
Guest
 
Posts: n/a
#12: Oct 12 '05

re: problem passing value



"Alf P. Steinbach" <alfps@start.no> wrote in message
news:434c5a02.139069625@news.individual.net...[color=blue]
> * dave:[color=green]
> > Why would a copy constructor cause the behavior I require, in this[/color][/color]
situation[color=blue][color=green]
> > isn't
> > passing by ref may only option?
> > "Dan Cernat" <dcernat@excite.com> wrote in message
> > news:1129060847.375092.105330@o13g2000cwo.googlegr oups.com...[color=darkred]
> > >
> > > don't toppost.[/color][/color]
>
> What part of "don't toppost" did you not understand?
>
> --
> A: Because it messes up the order in which people normally read text.
> Q: Why is it such a bad thing?
> A: Top-posting.
> Q: What is the most annoying thing on usenet and in e-mail?[/color]

Sorry, was in a hurry.


Closed Thread