473,544 Members | 1,959 Online

# cout problem

I have a function like this:
double MR::average_len ght_edge_on_ele ment(unsigned long element_number,
Mat_DP &element_metric , Distributed_Mes h &mesh )
with this function, I obtain two different results depending typically
to the cout stream.
there is here an extract of this function:
for (counter_i=0;co unter_i<mesh.di m;counter_i++)
{

for (counter_j=coun ter_i+1;counter _j<mesh.dim+1;c ounter_j++)
{
average_lenght+ =MR::square_len ghtM_edge(id_i, id_j,
element_metric, mesh);
// cout << endl; // false result
cout << endl; //true result
}
}

with a cout I obtain the correct result but without it, I get an other
value.
how can you explain this behaviour
Thanks
Nov 29 '05 #1
4 1365
Youssef Mesri wrote:
I have a function like this:
double MR::average_len ght_edge_on_ele ment(unsigned long element_number,
Mat_DP &element_metric , Distributed_Mes h &mesh )
with this function, I obtain two different results depending typically
to the cout stream.
there is here an extract of this function:
for (counter_i=0;co unter_i<mesh.di m;counter_i++)
{

for (counter_j=coun ter_i+1;counter _j<mesh.dim+1;c ounter_j++)
{
average_lenght+ =MR::square_len ghtM_edge(id_i, id_j,
element_metric, mesh);
// cout << endl; // false result
cout << endl; //true result
}
}

with a cout I obtain the correct result but without it, I get an other
value.
how can you explain this behaviour

Too little code to know what's going on. Can you post a compilable
example of the problem.
Nov 29 '05 #2
Youssef Mesri wrote:
I have a function like this:
double MR::average_len ght_edge_on_ele ment(unsigned long element_number,
Mat_DP &element_metric , Distributed_Mes h &mesh )
with this function, I obtain two different results depending typically
to the cout stream.
there is here an extract of this function:
for (counter_i=0;co unter_i<mesh.di m;counter_i++)
{

for (counter_j=coun ter_i+1;counter _j<mesh.dim+1;c ounter_j++)
{
average_lenght+ =MR::square_len ghtM_edge(id_i, id_j,
element_metric, mesh);
// cout << endl; // false result
cout << endl; //true result
}
}

with a cout I obtain the correct result but without it, I get an other
value.
how can you explain this behaviour

I can't. Not enough information. However, you can experiment some more
to see what it depends on. Try disabling optimization when compiling only
the module containing your code. See if behaviour changes _without_ cout.
If it does, blame the compiler and keep optimization disabled.

V
Nov 29 '05 #3
Victor Bazarov wrote:
Youssef Mesri wrote:
I have a function like this:
double MR::average_len ght_edge_on_ele ment(unsigned long
element_number, Mat_DP &element_metric , Distributed_Mes h &mesh )
with this function, I obtain two different results depending typically
to the cout stream.
there is here an extract of this function:
for (counter_i=0;co unter_i<mesh.di m;counter_i++)
{

for (counter_j=coun ter_i+1;counter _j<mesh.dim+1;c ounter_j++)
{
average_lenght+ =MR::square_len ghtM_edge(id_i, id_j,
element_metric, mesh);
// cout << endl; // false result
cout << endl; //true result
}
}

with a cout I obtain the correct result but without it, I get an other
value.
how can you explain this behaviour

I can't. Not enough information. However, you can experiment some more
to see what it depends on. Try disabling optimization when compiling only
the module containing your code. See if behaviour changes _without_ cout.
If it does, blame the compiler and keep optimization disabled.

V

Thanks Vector,
when I disable optimization it works. but I dont understand why this
happened!! it's wrong !!
Nov 29 '05 #4
Youssef Mesri wrote:
Victor Bazarov wrote:
[...] Try disabling optimization when compiling
only
the module containing your code. See if behaviour changes _without_
cout.
If it does, blame the compiler and keep optimization disabled.

V
Thanks Vector,

You're welcome, Yoossef.
when I disable optimization it works. but I dont understand why this
happened!! it's wrong !!

Yes, it is. But keep in mind, though, that compilers are also simply
software written by people, who are allowed to make mistakes, just like
you and I. Optimization of generated machine code is a difficult task to
accomplish, and the code optimizer can simply stumble, and forget to make
the code write something to memory... That's what I once found in VC++
(Microsoft's compiler), for example, and once in aCC (HP's compiler).
It usually happens in a rather large module, which also probably includes
a ton of other C++ code from headers. To fix it you may not need to drop
optimization altogether, just bring it down a level or two.

Good luck!

V
Nov 29 '05 #5

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