problem with dynamic memory

hi to all ok this is my problem. Im`m learning to program by my own so i have a little issue woth memory that i cannot figure out so im asking for a little help here... the program that i am triing to build is a program that ask the user to imput an array size then the program must create this array, filling with random integers and output it in screen. it compiles ok right now but when i run boxes to compile appear or error box when number imput its > 5 if it its 1 it runs without problems. you`l undertand better if you try and run it.. thx in advance

#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+1;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;
}
Use [code][/code] tags please.

Anyway, you have a couple of errors:

1) The size of the array you make goes from 0 - (size - 1), so in your for loop you want to loop from i = 0; i < size; only
2) If you want to use new to make an array, you must use [ ], not ( ):
bobby = new int[numero];
3) If you change the above then your delete [] bobby; should work.
Last edited on
thx the () was a stupid mistake that fix all the program but if i put delete[] bobby the error apears again can you help me again plz
Topic archived. No new replies allowed.