problem passing value | | |
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 | | | | 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 | | | | 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? | | | | 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 | | | | 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] | | | | 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. | | | | 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? | | | | 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 | | | | 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? | | | | 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] | | | | 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? | | | | 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. |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,449 network members.
|