 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Tue Jul 11, 2006 2:37 am Post subject: position of vector declaration |
|
|
Hello at all,
I am puzzled by a message in my compiler (visual c++ 8)
This is the smallest fragment that shows the problem:
#include <iostream>
#include <stdlib.h>
double currentVal[3];
int main(void)
{
for (int i = 0 ; i < 10 ; ++i)
{
double oldVal[3]; ///--------IS THIS DEFINITION CORRECTLY PUT?
//in the real application the current val vector is computed in
some way...
//here I simulate
for (int j = 0 ; j < 3 ; ++j)
{
currentVal[j] = (double)rand() / RAND_MAX;
}
if (i != 0)
{
//not first pass
std::cout << "old Val: " << oldVal[0] << " " <<
oldVal[1] << " " << oldVal[2] << std::endl;
//doSomethingWithCurrentValAndOldVal();
}
//now I copy currentVal in oldVal
memcpy(oldVal, currentVal, sizeof(oldVal));
} //-----------it seems that here, in visual C++ 8, the oldVal
//-----------goes out of scope
return 0;
}
This program is compiled correctly, but when I run it I obtain a
message that says
that oldVal (in the cout statement) is used without being defined.
Stepping in debug it seems (as I wrote in the comment) that oldVal goes
out
of scope when the program ends a cycle of the for.
Putting oldVal outside the for is ok, but I am puzzled because I
thought that
variables could be defined in the for statement and they are valid
throught it, from begin
to end of the for, not only for one iteration only.
The same program, in Dev-c++ 4.9.9.2 runs fine.
Thanks at all,
Sergio
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Carl Barron Guest
|
Posted: Tue Jul 11, 2006 7:29 am Post subject: Re: position of vector declaration |
|
|
In article <1152514276.725370.91860 (AT) b28g2000cwb (DOT) googlegroups.com>,
<sergiopeffe-news (AT) yahoo (DOT) it> wrote:
| Quote: | Hello at all,
I am puzzled by a message in my compiler (visual c++ 8)
This is the smallest fragment that shows the problem:
#include <iostream
#include <stdlib.h
double currentVal[3];
int main(void)
{
for (int i = 0 ; i < 10 ; ++i)
{
double oldVal[3]; ///--------IS THIS DEFINITION CORRECTLY PUT?
//in the real application the current val vector is computed in
some way...
//here I simulate
for (int j = 0 ; j < 3 ; ++j)
{
currentVal[j] = (double)rand() / RAND_MAX;
}
if (i != 0)
{
//not first pass
std::cout << "old Val: " << oldVal[0] << " "
oldVal[1] << " " << oldVal[2] << std::endl;
//doSomethingWithCurrentValAndOldVal();
}
//now I copy currentVal in oldVal
memcpy(oldVal, currentVal, sizeof(oldVal));
} //-----------it seems that here, in visual C++ 8, the oldVal
//-----------goes out of scope
// correct it is supposed to as written!! It is never filled befor |
// sent to cout above,
| Quote: |
return 0;
}
As written with the double oldValues[3]; inside the for(int i = 0, |
.....) loop the array is local to the current loop iteration that is it
goes in and out of scope one each iteration. As a result the memcpy()
at the end of the loop does nothing constructive. [it fills the array
and then it is disposed of at the end of the loop.] The array
declaration belongs outside the loop if it is supposed to hold the
results of the previous iteration of the for(...) loop. Two solutions
come to mind
1) place the declaration outside the for loop,
2) better is to load oldValues with the currentValues at the top of
the loop.
double CurrentValues[3] = {0.,0,,0,}; // so CurrentValues is
initialized before use.
for(int i= ...)
{
double oldValues[3];
memcpy(oldValues,CurrentValues,sizeof(oldValues));
calculate_new_values(currentValues);
std::cout << oldValues[0] << ...
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|