I´m trying to create a template function that generates an array of random numbers of specific type (that happens to be int here), accepting pointer as a reference. The code is not ready, I know, but if I can´t get this thing working, I can´t continue at all. Help, please?
At first glance, you seem to be trying to write data into memory that does not belong to you.
int *numbers = 0;
Create a pointer to an int, and at this stage make it a null pointer. So far, so good.
storage_array[i] = rand() % range_end;
Try to assign a value to storage_array[0] - wait a second, at this stage, storage_array is still a null pointer. You've not allocated any memory for your array of integers.
...accepting pointer as a reference.
Be careful with your terminology; "a reference" has a specific meaning in C++, in the context of pass-by-reference, and you're not doing that here. You're passing a pointer, by value.
main()'s ok, it the elements parameter that's been wrongly specified. The code should be something like:
1 2 3 4 5 6 7 8 9
int main()
{
srand(time(0));
constint N = 10;
int numbers[N];
unique_rand<int>(numbers, N, 16, 32);
return 0;
}
As for unique_rand(), why the inner loop? You're not really testing your code by using type int as rand() returns an int. Also the random code should be:
I created inner for loop, because I also want to check that there won´t be same numbers in my random number array. This is the thing I forgot to mention, by the way.
Now I got it: I have to use memory allocation for my array, like this:
I see no warnings or errors now. But, the program doesn´t work as I wanted it to work. It prints random numbers yes, but it doesn´t print random numbers from the range I wanted the numbers to be printed. Man, this sucks...