473,320 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

Oct 11 '05 #1
11 1545

dave wrote:
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


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

/dan

Oct 11 '05 #2
* dave:
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);
Possible buffer overrun.
totgrossdlrval+=tf[i].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.

MessageBox(NULL,"Monthly return for portfolio is "
+ (CString)converter,"C++ Debugger",NULL);

}


--
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?
Oct 11 '05 #3
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" <sp*****@hotmail.com> wrote in message
news:iu*****************@bignews5.bellsouth.net...
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

Oct 11 '05 #4

don't toppost.
dave wrote:
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

Oct 11 '05 #5
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.

Oct 11 '05 #6

"Dan Cernat" <dc*****@excite.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...

dave wrote:
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


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

/dan

no copy constructor isnt't there a c++ defualt?
Oct 11 '05 #7

dave wrote:
"Dan Cernat" <dc*****@excite.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...

dave wrote:
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


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

Oct 11 '05 #8
In article <11**********************@g47g2000cwa.googlegroups .com>,
<An**********@gmail.com> wrote:
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 :)
--
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?
Oct 11 '05 #9
Why would a copy constructor cause the behavior I require, in this situation
isn't
passing by ref may only option?
"Dan Cernat" <dc*****@excite.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...

don't toppost.
dave wrote:
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

Oct 12 '05 #10
* dave:
Why would a copy constructor cause the behavior I require, in this situation
isn't
passing by ref may only option?
"Dan Cernat" <dc*****@excite.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...

don't toppost.


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?
Oct 12 '05 #11

"Alf P. Steinbach" <al***@start.no> wrote in message
news:43****************@news.individual.net...
* dave:
Why would a copy constructor cause the behavior I require, in this situation isn't
passing by ref may only option?
"Dan Cernat" <dc*****@excite.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...

don't toppost.


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.
Oct 12 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
6
by: Olaf Martens | last post by:
Greetings! Please consider the following piece of program code (note that I have stripped quite a lot of code here): int foo(void) { unsigned short l_valbuf; // address of this goes to...
7
by: Harolds | last post by:
The code below worked in VS 2003 & dotnet framework 1.1 but now in VS 2005 the pmID is evaluated to "" instead of what the value is set to: .... xmlItems.Document = pmXML // Add the pmID...
1
by: Hans [DiaGraphIT] | last post by:
Hi! I have problem with passing data to custom action. I don't know what wrong I'm doing. I'm trying to follow the steps in the walkthrough: "Passing Data to a Custom Action" ...
5
by: Kevin Yu | last post by:
hi all since the DateTime can't be assign null, the min value is set to 1901 or something, if in a application design, the date field can be null, so in between the UI and the DB, the business...
12
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
5
by: Steve | last post by:
Hi, I currently have a problem passing a variable value from one page to another. Once a form submit button is pressed java pops up a window and displays some information. The problem being is...
17
by: Ashwin | last post by:
hi guys, i have overloaded the << operator.as shown below. ostream& operator<<(ostream &out, const student &a) { out<<a.idno; out<< " " ; // out<< a.name; out<< " " ; // out<< a.marks...
4
by: kinaxx | last post by:
Hello, now I'm learning progamming language in university. but i have some question. in textbook. says there are four passing Mechanism 1) pass by value (inother words : call by value) 2)...
11
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.