dynamic memory array deleting problem

hi i have a problem with my code everything works fine the program do what it does but the problem is that i cant delete the array that i just create it shows an error message box. can you help me here is my code. the program was made to create an array which size is given by the user and then output the content to the screen.

NOTE: the part that the code needs to fix its written in comment

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
47
48
49
50
51
52
53
54
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

void PrintArray(int* array, int size);
void RandomArrayFill(int* array, int size);


void RandomArrayFill(int* array, int size)
{
	int i=0;
	for(i=0;i<=size;i++)
	{
		array[i]= rand() % 101;
	}
}
void PrintArray(int* array, int size)
{
	int i=0;
	for(i=0;i<size+1;i++)
	{
		if(i==size)
		{
			cout <<array[i] << "}" << endl << endl;
			break;
		}
		cout << array[i] << ", " ;
		
	}
}

void main()
{
	int numero=0; 
	int* bobby=0; 
	srand( time(0) );

	do
	{
	cout << "Enter the size of an array to create: ";
	cin >> numero;
	bobby = new int[numero];
	RandomArrayFill(bobby, numero);
	cout << endl << endl << "Creating array and filling it with random numbers...";
	cout << "Array = {";
	PrintArray(bobby, numero);
	//delete [] bobby;
	//bobby=0;
	}
	while(numero!=0);
	cout << "NULL Array" << endl;
}


thx in advance
What is your actual error?
i want to delete the created array after the function PrintArray out put the result to the screen
Your delete[] lines are fine. It is your RandomArrayFill and PrintArray functions that are broken. They need to go from 0~(size-1) only.
Topic archived. No new replies allowed.