Re: Double Indirection
From: Mike Wahler (mkwahler_at_mkwahler.net)
Date: 07/08/04
- Previous message: who: "Re: I need more eyes on this one."
- Next in thread: Francis Glassborow: "Re: Double Indirection"
- Maybe reply: Francis Glassborow: "Re: Double Indirection"
- Maybe reply: Francis Glassborow: "Re: Double Indirection"
- Maybe reply: Mike Wahler: "Re: Double Indirection"
- Maybe reply: rossum: "Re: Double Indirection"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 08 Jul 2004 04:09:29 GMT
"cpp072004" <cpp072004@yahoo.com> wrote in message
news:b6568e3f.0407071936.77803575@posting.google.com...
> Hello. Given the following code below, can anyone tell me why the
> output from "rates" is correct, but the output from "yieldRates" is
> incorrect?
Your whole program is incorrect, it produces undefined
behavior (gave 'illegal operation' error on my machine)
> And if I needed to use double indirection (no pun
> intended) how would I accomplish it?
>
> #include <iostream>
>
> using namespace std;
>
> void main()
int main()
{
> double *rates = new double[2];
>
> rates[0] = 2.2f;
> rates[1] = 3.33f;
The array elements are type 'double'. Why are you using
type 'float' initializers? It won't harm anything, but
they'll be converted to 'double' anyway, so what's the point?
>
> cout << "rates [0] = " << rates[0] << endl;
> cout << "rates [1] = " << rates[1] << endl;
>
> double **yieldRates = new double*[2];
>
> *yieldRates[0] = 1.2f;
> *yieldRates[1] = 2.32f;
Both of these statements yield :-) undefined behavior.
You're trying to write to memory not owned by your program,
and you're trying to assign a floating point value to a
pointer.
>
> cout << "yieldRates [0] = " << *yieldRates[0] << endl;
> cout << "yieldRates [1] = " << *yieldRates[1] << endl;
Two more cases of undefined behavior.
>
> delete rates;
> delete yieldRates;
Yet two more cases of undefined behavior.
> }
>
> yields:
>
> rates [0] = 2.2
> rates [1] = 3.33
> yieldRates [0] = 2.32
> yieldRates [1] = 2.32
It could yield anything (or nothing) at all, or
anything in between.
Carefully compare this with yours:
#include <iostream>
#include <ostream>
using namespace std;
int main()
{
double *rates = new double[2];
rates[0] = 2.2f;
rates[1] = 3.33f;
cout << "rates [0] = " << rates[0] << endl;
cout << "rates [1] = " << rates[1] << endl;
double **yieldRates = new double*[2];
yieldRates[0] = new double(1.2);
yieldRates[1] = new double(2.32);
cout << "yieldRates [0] = " << *(yieldRates[0]) << endl;
cout << "yieldRates [1] = " << *(yieldRates[1]) << endl;
delete[] rates;
delete yieldRates[0];
delete yieldRates[1];
delete[] yieldRates;
return 0; /* optional, but IMO 'good style' */
}
Finally, you needn't be messing around with arrays at all.
Use containers.
-Mike
- Previous message: who: "Re: I need more eyes on this one."
- Next in thread: Francis Glassborow: "Re: Double Indirection"
- Maybe reply: Francis Glassborow: "Re: Double Indirection"
- Maybe reply: Francis Glassborow: "Re: Double Indirection"
- Maybe reply: Mike Wahler: "Re: Double Indirection"
- Maybe reply: rossum: "Re: Double Indirection"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|