473,804 Members | 3,259 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem passing value

void CalcPortGrossRe t(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.grossenddlrv al=totgrossdlrv al;
tp.ret[cmonth]=(tp.grossenddl rval-tp.grossdlrval)/tp.grossdlrval;
sprintf(convert er,"%.5f",tp.re t[cmonth]);
MessageBox(NULL ,"Monthly return for portfolio is "
+ (CString)conver ter,"C++ Debugger",NULL) ;

}

after CalcPortGrossRe t is called with
CalcPortGrossRe t(thefunds,3,th eport,im);
and returns,
theport.grossen ddlrval has garbage in it.

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

thanks

Oct 11 '05 #1
11 1575

dave wrote:
void CalcPortGrossRe t(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.grossenddlrv al=totgrossdlrv al;
tp.ret[cmonth]=(tp.grossenddl rval-tp.grossdlrval)/tp.grossdlrval;
sprintf(convert er,"%.5f",tp.re t[cmonth]);
MessageBox(NULL ,"Monthly return for portfolio is "
+ (CString)conver ter,"C++ Debugger",NULL) ;

}

after CalcPortGrossRe t is called with
CalcPortGrossRe t(thefunds,3,th eport,im);
and returns,
theport.grossen ddlrval has garbage in it.

While in function tp.grossenddlrv al is correct.
tp.grossenddlrv al 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 CalcPortGrossRe t(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.grossenddlrv al=totgrossdlrv al;
'tp' is passed by value into this function, this should have no effect.

tp.ret[cmonth]=(tp.grossenddl rval-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(convert er,"%.5f",tp.re t[cmonth]);
Possible buffer overrun.

MessageBox(NULL ,"Monthly return for portfolio is "
+ (CString)conver ter,"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*****@hotmai l.com> wrote in message
news:iu******** *********@bigne ws5.bellsouth.n et...
void CalcPortGrossRe t(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.grossenddlrv al=totgrossdlrv al;
tp.ret[cmonth]=(tp.grossenddl rval-tp.grossdlrval)/tp.grossdlrval;
sprintf(convert er,"%.5f",tp.re t[cmonth]);
MessageBox(NULL ,"Monthly return for portfolio is "
+ (CString)conver ter,"C++ Debugger",NULL) ;

}

after CalcPortGrossRe t is called with
CalcPortGrossRe t(thefunds,3,th eport,im);
and returns,
theport.grossen ddlrval has garbage in it.

While in function tp.grossenddlrv al is correct.
tp.grossenddlrv al 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.goo glegroups.com.. .

dave wrote:
void CalcPortGrossRe t(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.grossenddlrv al=totgrossdlrv al;
tp.ret[cmonth]=(tp.grossenddl rval-tp.grossdlrval)/tp.grossdlrval;
sprintf(convert er,"%.5f",tp.re t[cmonth]);
MessageBox(NULL ,"Monthly return for portfolio is "
+ (CString)conver ter,"C++ Debugger",NULL) ;

}

after CalcPortGrossRe t is called with
CalcPortGrossRe t(thefunds,3,th eport,im);
and returns,
theport.grossen ddlrval has garbage in it.

While in function tp.grossenddlrv al is correct.
tp.grossenddlrv al 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.goo glegroups.com.. .

dave wrote:
void CalcPortGrossRe t(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.grossenddlrv al=totgrossdlrv al;
tp.ret[cmonth]=(tp.grossenddl rval-tp.grossdlrval)/tp.grossdlrval;
sprintf(convert er,"%.5f",tp.re t[cmonth]);
MessageBox(NULL ,"Monthly return for portfolio is "
+ (CString)conver ter,"C++ Debugger",NULL) ;

}

after CalcPortGrossRe t is called with
CalcPortGrossRe t(thefunds,3,th eport,im);
and returns,
theport.grossen ddlrval has garbage in it.

While in function tp.grossenddlrv al is correct.
tp.grossenddlrv al 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************ **********@g47g 2000cwa.googleg roups.com>,
<An**********@g mail.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.goo glegroups.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

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

Similar topics

58
10188
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 code... TCHAR myArray; DoStuff(myArray);
6
1414
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 another function Objtype *l_item; // I want to get this one's data!
7
2872
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 parameter to the XSLT stylesheet XsltArgumentList xsltArgList = new XsltArgumentList(); xsltArgList.AddParam("pmID", "", pmID); xmlItems.TransformArgumentList = xsltArgList;
1
5356
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" http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsintro7/html/vxwlkwalkthroughpassingdatatocustomaction.asp The error I recieve is: "The savedSate dictionary does not contain the expected values and might
5
2055
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 logic has to take care of the conversion?? e.g when inserting DateTime.minvalue into db, (especially when using store precedure, it seems that pass null parameter to a store precedure cause problem.), the store procedure must translate the minvalue...
12
2689
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
2427
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 variables value needs to be passed from the main page to this popup window ... but it isn't. why? the submit button one the PHP page is
17
2337
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 << endl;
4
9124
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) pass by reference (inother words: call by reference) 3) pass by value-result <- i have question this subject . 4) pass by name
11
3368
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 ways of passing an array. In the following code, fun1(int a1) - same as fun1(int* a1) - where both are of the type passed by reference. Inside this function, another pointer a1 is created whose address &a1 is different from that of the passed...
0
9704
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9572
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10319
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10303
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10070
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7608
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6845
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5508
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4282
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.