LuckySlevin
LuckySlevin

Reputation: 745

Initializing and using dynamic arrays in C++

In my code, I am trying to create a dynamic array with initArray function, and in main I would like to use this initialized array. However, whenever i called the initialized array in main, it is giving me an error.

Here is what i tried:

void main() 
{
    int *a = NULL;
    int n;
    cout<<"Enter size:";
    cin>>n;
    initArray(a,n);
    for(int j=0;j<n;j++)
    {
        cout<<a[j]<<endl;//Crashes here
    }
}
void initArray(int *A, int size)
{
    srand((unsigned)time(0));
    A = new int[size];
    for(int i=0;i<size;i++)
    {
        A[i] = rand()%10;
    }
}

When i do initArray part in main, it works. What am i doing wrong?

Upvotes: 2

Views: 3709

Answers (5)

Hicham
Hicham

Reputation: 979

you cna change the dclaration of initArray :

void initArray(int* &A, int size)

in your initial code, the array is not filled in. because, A array is a copy of the main a array. so the a array remains equals to null. you have then the choice between of passing a by reference "int* &A" as i did or by adress : "int** A"

Upvotes: 0

cpx
cpx

Reputation: 17557

You are passing a copy of your pointer to initArray, make it a reference initArray(int *&A, int size)

#include <iostream>
using namespace std;

void initArray(int *&A, int size); // put the declaration of function here


int main() 
{
    int *a = 0;

    int n;
    cout << "Enter size:";
    cin >> n;

    initArray(a,n);

    for(int j=0;j<n;j++)
    {
        cout<<a[j]<<endl;
    }

    delete[] a; // delete your array before exiting the program
}

// pass it as a reference to pointer
void initArray(int *&A, int size)
{
    srand((unsigned)time(0));
    A = new int[size];

    for(int i=0;i<size;i++)
    {
        A[i] = rand()%10;
    }
}

Upvotes: 1

fredoverflow
fredoverflow

Reputation: 263118

You need to put the initArray function definition above main or at least declare it above main.

Also note that the assignment A = new int[size]; only modifies the local variable A inside the function initArray. It has no effect on the a inside main because pointers are passed by value. You need to either pass a reference or a pointer to the pointer, or better yet, return the pointer:

int* initArray(int size)   // note: one less parameter
{
    srand((unsigned)time(0));
    int* A = new int[size];
    for(int i=0;i<size;i++)
    {
        A[i] = rand()%10;
    }
    return a;   // note: return the pointer
}

And then write a = initArray(n); inside main. Also, don't forget delete[] a; inside main!

And of course the usual advice, don't use raw pointers to dynamic arrays, use std::vector instead. But since I guess this is homework for a crappy C++ course, you probably have no choice.

Upvotes: 3

Xeo
Xeo

Reputation: 131789

What am i doing wrong?

Not using std::vector is what you're doing wrong.

That aside, assuming this is for learning or homework or something:

initArray(a,n);

This line copies the int pointer a. The copy inside the function gets assigned, and the one in main will remain empty. You need to use pass-by-reference, either through C++ references or C style with pointers:

void initArray(int*& a, int size){
  // everything the same
}

This will modify the int pointer in main without any other changes.

void initArray(int** a, int size){
  // need to dereference the pointer-to-pointer to access the int pointer from main
  *a = new int[size];

  for(/*...*/){
   (*a)[i] = /*...*/;
  }
}

For this one, you'll need to change the call side too:

initArray(&a, n); // pass pointer to a

Now one last thing: main doesn't even know that initArray even exists. You need to put it above main or atleast forward declare it:

void initArray(int*& a, int size); // forward declaration

int main(){
  // ...
}

void initArray(int*& a, int size){
  // ...
}

And one last thing, you'll need to delete[] the array in main.

Upvotes: 5

Vlad
Vlad

Reputation: 18633

I see two problems:

  1. the function accepts a pointer. When you write A = ... you're only changing the copy of the pointer that gets passed to you by value. You could use void initArray(int* &A, int size) instead, or have the function return a pointer.

  2. if that's the full code, you might need a forward declaration of the initArray function.

Upvotes: 7

Related Questions