Recursion and Arrays

It's quite interesting what is happening here and I would like to know why. After the recursion is ran 10 times, the *array = x and I have no idea why. I only want the program to return if when the element in the array is equal to x. However, when the userInput is equal to 2 or 3 or 4, the recursion is ran 10 times, *array = x, and the function returns. Any suggestions why this happening and how to fix it?

I hope my questions made sense...

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
31
32
33
34
35
36
37
38
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cstring>
using namespace std; 

void findIndexOfArray (int *array, int x)
{
	cout << "array is: " << *array << endl;
	cout << "x is: " << x << endl; 
	if (*array == x)
	{
		cout << "It's going in! " << endl;
		return; 
	}
	findIndexOfArray (array + 1, x);
}

int main() 
{
	int userInput; 
	int array[10]; 
	for (int i = 0; i < 10; i++)
	{
		array[i] = 5 + i;
		cout << array [i] << endl;
	}

	cout << *(array + 9); 

	cin >> userInput; 
	while (userInput > 0)
	{
		findIndexOfArray (array, userInput); 
		cin >> userInput; 
	}
	return 0; 
}
The array doesn't contain 2, 3 or 4 so the recursion never stops. It just keeps going, accessing memory outside the array, which will eventually lead to a program crash if the value you are looking for is not found.
Last edited on
@Peter87

I see, but what's weird is that the array after the last array is automatically set to equal to x.

array is: 5
x is: 1
array is: 6
x is: 1
array is: 7
x is: 1
array is: 8
x is: 1
array is: 9
x is: 1
array is: 10
x is: 1
array is: 11
x is: 1
array is: 12
x is: 1
array is: 13
x is: 1
array is: 14
x is: 1
array is: 1
x is: 1


Why does the array = x?
Maybe the data stored right after the array happens to represent the int value 1. It's just pure coincidence. You should not rely on this behaviour. The C++ standard simply says the behaviour is undefined in this situation which means anything is allowed to happen. What you should do is to avoid accessing elements outside the array in the first place.
Last edited on
@Peter87,

I see. Thank you so much for your help!

Best, Jae Kim
Maybe the data stored right after the array happens to represent the int value 1. It's just pure coincidence. You should not rely on this behaviour. The C++ standard simply says the behaviour is undefined in this situation which means anything is allowed to happen. What you should do is to avoid accessing elements outside the array in the first place.


This can't be a coincidence. I just ran the program, and no matter what value of x I input, the last recursion always sets the *array equal to x.

The C++ Shell produces this:
array is: 5
x is: 1
array is: 6
x is: 1
array is: 7
x is: 1
array is: 8
x is: 1
array is: 9
x is: 1
array is: 10
x is: 1
array is: 11
x is: 1
array is: 12
x is: 1
array is: 13
x is: 1
array is: 14
x is: 1
array is: 4196950
x is: 1
array is: 0
x is: 1
array is: 0
x is: 1
array is: 0
x is: 1
array is: 0
x is: 1
array is: 0
x is: 1
array is: 4196950
x is: 1
array is: 0
x is: 1
array is: -386274491
x is: 1
array is: 26537
x is: 1
array is: 0
x is: 1
array is: 0
x is: 1
array is: 1940665048
x is: 1
array is: 30172
x is: 1
array is: 0
x is: 1
array is: 1
x is: 1
It's going in! 
Arslan7041 wrote:
This can't be a coincidence. I just ran the program, and no matter what value of x I input, the last recursion always sets the *array equal to x.

Maybe the variable userInput (or x) is stored right after the array in memory.
Topic archived. No new replies allowed.