junk return on passing an array

i passed an array from a function from a class and i am using the object of that class in other file but when i pass it the values of that array are coming ssome junk values (i think) i ll show u the usage :
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
// randomno.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
//#include<iostream>
#include<ctime>

using namespace std;

class randomnogen
{
public:
int& generate()
{
	int j=0,min=1, max=35,Q_Rand[120]={0},Q_FRand[120]={0};
	//Q_Rand[0] = 0;
	srand(time(NULL));
	for(int i=1; i<120; i++)
	{
		Q_Rand[i] = (rand()%(max-min + 1)) + min;
		Q_FRand[i]=Q_Rand[i];
		//cout << Q_Rand <<endl ;
		for( j=0; j<i ;j++)
		{
			//cout << j << endl;
			if(Q_Rand[i]==Q_Rand[j])
				Q_FRand[i]=(rand()%(max-min + 1)) + min;
			else
				continue;
		}
	}
	return *Q_FRand;
}
};		  


then i used it in my main file like this:
1
2
int* Q_Rand = &o_random_gen.generate();
o_SciTech.showQuestion(Q_Rand[i],main_quesfname);

the value of Q_Rand comes out to be some very large value sometimes negative sometimes positive (i.e a junk )
Here you are passing(by return value of generate()) a reference to a variable that does not exist anymore.
Q_FRand is a local array, and so when the function returns, it is destroyed(since it is more likely, stored in stack memory). You then return a reference to the first element of a destroyed array, which is the "junk"

You could make Q_FRand a class variable.

Aceix.
You are violating very basic rule, never return local variables/values by reference.

If you want to return array of ints, pass it by reference as a parameter.
OR if you have a C++11 capable compiler (VS 2012~, clang 3.4~, GCC 4.8~) then return the entire (local) array by value using std::move.

Read C++11 "return value optimization" and "move semantics"
@aceix
i declared it as a class variable even then it is showing some junk values...what now?
replace "&" on line 13 with "*" and remove "&" on line 1 of second listing and see what happens.

Anyway, read on return value optimizations, functions, local variables etc
@codewalker-
how can i pass an array without using a pointer, i can only access its address if i pass it..?
still showing junk.........:(
Now you see that this code and your original code have same results.
This again proves you cannot return either reference to or the local variable itself from a function. As Aceix said the location which will be accessed after the function returns will no longer be available, the memory used by that variable (whether it is an array of whatever else) belonged to the function, which has been cleaned up after the function exited.

You have to allocate memory
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
int * generate(const int size)
{
	int j=0,min=1, max=35;
	int *Q_FRand = new int[size];
	int *Q_Rand = new int[size];

	//Q_Rand[0] = 0;
	srand(time(NULL));
	for(int i=0; i<size; i++)
	{
		Q_Rand[i] = (rand()%(max-min + 1)) + min;
		Q_FRand[i]=Q_Rand[i];
		//cout << Q_Rand <<endl ;
		for( j=0; j<i ;j++)
		{
			//cout << j << endl;
			if(Q_Rand[i]==Q_Rand[j])
				Q_FRand[i]=(rand()%(max-min + 1)) + min;
			else
				continue;
		}
	}

	delete [] Q_Rand;
	return Q_FRand;
} 


and in main (or some place else)
int *Q_Rand = generate(120);

thank you very much...it worked...:)
one more thing do you know how to generate at least 35 distinct random nos between 1 to 35 because that's what i was trying to do in my above code but it still repeats after 9 - 10 nos...is there a way out because i also searched through the internet but couldn't find one....?
Read here http://www.cplusplus.com/reference/random/
especially about std::uniform_int_distribution<int>
i read it..it provides a no of distributions but i am not sure that it will generate "distinct" random nos.....
Codewalker's solution is good, if a little vague.

Since you cannot have any duplicates, the easiest way to ensure that is to start with a fixed (non-random) array where each value is unique:

1
2
3
4
int foo[] = {1,2,3,4, ...

// or better yet... create this using a loop so you don't have to manually
//  input each number 


You can then use something like random_shuffle to rearrange all the elements in the array. This will ensure the order is random, and that you will not have any duplicate elements.
Topic archived. No new replies allowed.