Unhandled exception...Access violation writing location

So im trying to write a program that takes the users input for the size of an array then creates an array of that size and fill it with random numbers.

BUT it fills the array with the same random number.

here is my 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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

void RandomArrayFill(int* array, int size);

int main()
{
	
	int size		= 0;

	cout << "Enter the size of an array to create: ";
	cin >> size; 

        int* userArray	= new int[size]

	cout <<endl <<  "Creating array and filling it with random numbers..." << endl;
	RandomArrayFill(userArray, size);

	cout << "Array = {";
	for(int i = 0; i < size; i++)
	{
		cout << userArray[i] << " ";
	}
	cout << "}";

        delete userArray;
	cin.get();
	cin.get();
	return 0;
}

void RandomArrayFill(int* array, int size)
{
	int* newArray = new int[size];

	for(int i = 0; i < size; i++)
		array[i] = newArray[i];
	for(int i = 0; i < size; i++)
	{
		srand(time(0));
		array[i] = rand() % 101;
	}
}
Last edited on
closed account (zb0S216C)
Your first argument to RandomArrayFill( ) is a pointer to and int, not an array. To pass an array, you need to swap your first argument with something like this: int Array[ ].

Wazzak
Last edited on
@Framework Actually, I believe that all of the following are the same:
1
2
3
void foo(int*);
void foo(int[]);
void foo(int[some_number]);


@OP The problem is that you pass userArray to your function. However, this array is set to 0, thus the exception about an access violation at location 0x0000000 (i.e. 0) when you try and access it. You need to initialize the array with a nonnull value before you try to use pointer or array operations with it.
Last edited on
@Xander How would i do this though? The point of the function is to fill the array. What would i initialize the array to?
You just have to allocate memory for it, for example as you do on line 34 for newArray. Note that you should use delete to free dynamically allocated memory when you're done with it.

In fact, what exactly are you trying to do in the function?The array newArray seems completely redundant. You can just allocate memory for your other array directly if that's what you're trying to do.
Oh thanks dude i got it but instead of filling the array with random numbers it fills it with the same random number. could you help me with that too?

EDIT: i edited the OP with my new code for you too see
Last edited on
Right then. Firstly, two small points:
1) You are missing a semicolon on your newly written allocation line :p
2) Strictly speaking, you should check that the user has entered an appropriate value (e.g. not negative and perhaps not too large to prevent excessive memory usage).

Now for the main thing. You are now allocating and deleting your array in main correctly. The remaining issues are within your other function. Here are my suggestions for adjusting that function.
 
int* newArray = new int[size];

What is this for? you don't need a second array if all you want is to fill the first one with values.

1
2
for(int i = 0; i < size; i++)
   array[i] = newArray[i];

You assign to each element of array the corresponding element of newArray. However, newArray's values have not been assigned, so they contain whatever happened to be in the memory when it was allocated. Furthermore, you then proceed to change the values of array in the next lines, so I'm not sure what this bit is supposed to achieve.

As far as I can see, all the previous code in this function is completely redundant, but it isn't causing your error. That is due to the following code:
1
2
3
4
5
for(int i = 0; i < size; i++)
{
	srand(time(0));
	array[i] = rand() % 101;
}

The numbers generated are not truly random. You keep seeding them with the value from time(0), which is only accurate to a second, I believe. So time(0) will probably yield the same value for each call in this loop. As you are seeding the random generator to the same value before each run, the subsequent call to rand() yields the same value.

Move your call to srand() out of the loop, so that the generator is seeded only once.
Thanks dude it works perfectly now!!

now just to get that windows project working hehe
Topic archived. No new replies allowed.