Problems with pointers

Hi, what´s wrong with this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <cstdlib>
#include <iostream>
#include <time.h>
using namespace std;

template<typename T>
void unique_rand(T *storage_array, int elements, T range_start, T range_end){
	for(int i=0; i<elements; i++){
		storage_array[i] = rand() % range_end;
		for(int j=0; j<i; j++){
			while(storage_array[i] < range_start || storage_array[i] == storage_array[j])
				storage_array[i] = rand() % range_end;
		}
	}
}

int main(){
	srand(time(0));
	int *numbers = 0;
	unique_rand<int>(numbers,4,16,32);
	return 0;
}


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.
Last edited on
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));

	const int 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:
 
storage_array[i] = rand() % (range_end - range_start) + range_start;


Clearly not your code...
Last edited on
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <cstdlib>
#include <iostream>
#include <time.h>
using namespace std;

template<typename T>
void unique_rand(T *storage_array, int elements, T range_start, T range_end){
	for(int i=0; i<elements; i++){
		storage_array[i] = rand() % range_end;
		for(int j=0; j<i; j++){
			while(storage_array[i] < range_start || storage_array[i] == storage_array[j])
				storage_array[i] = rand() % range_end;
		}
	}
}

int main(){
	srand(time(0));
	int *numbers = new int[4];
	unique_rand<int>(numbers,4,16,32);
        for(int i=0; i<4; i++) cout << numbers[i] << endl;
        delete [] numbers;
	return 0;
}


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...

Any suggestions?
Last edited on
yes, read near the end of kbw's post ;-)
Oops, my bad. It seems to work now, but could somebody please explain me why it works?

Current 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
#include <cstdlib>
#include <iostream>
#include <time.h>
using namespace std;

template<typename T>
void unique_rand(T *storage_array, int elements, T range_start, T range_end){
	for(int i=0; i<elements; i++){
		storage_array[i] = rand() % (range_end - range_start) + range_start;
		for(int j=0; j<i; j++){
			while(storage_array[i] < range_start || storage_array[i] == storage_array[j])
				storage_array[i] = rand() % (range_end - range_start) + range_start;
		}
	}
}

int main(){
	srand(time(0));
	int *numbers = new int[4];
	unique_rand<int>(numbers,4,16,32);
        for(int i=0; i<4; i++) cout << numbers[i] << endl;
        delete [] numbers;
	return 0;
}
Last edited on
Topic archived. No new replies allowed.