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 :
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 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"
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.
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....?
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.