¡Hi!
I come from another forum whith the next problem, very strange, that nobody has been able to explain...
Of course, there are alternatives, but what i want is to understand why that code causes a runtime error.
Here is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
#include <iostream>
#include <vector>
using namespace std;
vector<int> fe;
int llegeix() {
char c;
cin >> c;
if (c == '0') return -1;
int k = fe.size();
fe.push_back(42);
//int aux = llegeix();
fe[k] = llegeix();
//fe[k] = aux;
return k;
}
int main()
{
llegeix();
for (int i = 0; i < fe.size(); ++i) cout << fe[i] << endl;
return 0;
}
|
If I try to show the values after de
fe[k] = llegeix() assignment (cout << fe[k]), I see that the fisrt values of the vector are 42.
I'm using g++ (MinGW), i have not tried to use other compiler, so i don't know if it is a general problem or if it only is a g++ trouble.
What is happening????
Thanks for your time!
PS: If I uncomment the two commented lines in the
llegeix funtion, and i comment the
fe[k] = llegeix() I find that there is not any runtime error...
Like
cire has said, i have not well explained the problem. Here is an explanation:
The recursive function
llegeix, reads characters from the inpunt untill 0 (zero) is introduced, and it returns the
fe vector's size when the function was called. In each call it is suposed to store
fe.size() + 1 in the vector's
fe.size() position.
If I do the next there is not problem:
1 2 3 4 5 6 7
|
int k = fe.size();
fe.push_back(a_value);
int aux = llegeix();
fe[k] = aux;
|
But if I directly assign the function's return value to the vector's last position, the program crashes. That is:
1 2 3 4 5
|
int k = fe.size();
fe.push_back(a_value);
fe[k] = llegeix();
|
this code makes the program to crash. The values stored in the vectro are not correct values. The function executes, returns and when i try to acces the vector's values it crashes.
I have checked the vector's size after the call to the recursive funcion, and it is ok, but trying to acces it's members causes the program to crash.
I don't understand why it happens. Can some one explain it?
¡Thanks!