problem passing value

D

dave

void CalcPortGrossRet(Funds tf[],int fsize,PortFolio tp,int cmonth)
{
int i=0;
float totgrossdlrval=0;
char converter[10];
while(i<fsize){
tf.enddlrval=(tf.dlrval*(tf.ret[cmonth]/100.0)+tf.dlrval);
totgrossdlrval+=tf.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
 
D

Dan Cernat

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


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

/dan
 
A

Alf P. Steinbach

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


Possible buffer overrun.
totgrossdlrval+=tf.enddlrval;
i++;
}
tp.grossenddlrval=totgrossdlrval;


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

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

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...

sprintf(converter,"%.5f",tp.ret[cmonth]);

Possible buffer overrun.
 
D

dave

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
 
D

Dan Cernat

don't toppost.

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? C++ is not Java or C#
Is this true ?
no, it is not true.
I'm a little confused.
Is this true ?
[snip]

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

or pass by reference.

/dan
 
A

AnonMail2005

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.
 
D

dave

Dan Cernat said:
void CalcPortGrossRet(Funds tf[],int fsize,PortFolio tp,int cmonth)
{
int i=0;
float totgrossdlrval=0;
char converter[10];
while(i<fsize){
tf.enddlrval=(tf.dlrval*(tf.ret[cmonth]/100.0)+tf.dlrval);
totgrossdlrval+=tf.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


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

/dan



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

Dan Cernat

dave said:
Dan Cernat said:
void CalcPortGrossRet(Funds tf[],int fsize,PortFolio tp,int cmonth)
{
int i=0;
float totgrossdlrval=0;
char converter[10];
while(i<fsize){
tf.enddlrval=(tf.dlrval*(tf.ret[cmonth]/100.0)+tf.dlrval);
totgrossdlrval+=tf.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


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

/dan



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


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
 
G

Greg Comeau

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.

<nitpick on>
Meaning that for your first sentence to be true, the last one can't be :)
 
D

dave

Why would a copy constructor cause the behavior I require, in this situation
isn't
passing by ref may only option?
Dan Cernat said:
don't toppost.

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? C++ is not Java or C#
Is this true ?
no, it is not true.
I'm a little confused.
Is this true ?
[snip]

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

or pass by reference.

/dan
 
A

Alf P. Steinbach

* dave:
Why would a copy constructor cause the behavior I require, in this situation
isn't
passing by ref may only option?

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

dave

Alf P. Steinbach said:
* dave:

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?

Sorry, was in a hurry.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top