-1

I am trying to pass an array to constructor in C++ but it is not working:

arrayClass(int *array,int size){
        a = new int[15];
        for(int i(0); i < size; i++) {
           this->a[i] = array[i];
        }
        this->size = size;
cout << "In second constructor" << endl;
    }

in main()

int array[3]={1,2,3};
arrayClass a2(array,3);
user2895589
  • 1,010
  • 4
  • 20
  • 33
  • 1
    Please make your question less vague. "Not working" can mean many things. Do you get errors? Which ones? Can you provide an [MCVE](http://stackoverflow.com/help/mcve)? – chris Nov 05 '14 at 05:22
  • 1
    you pass in an array and its length, but in the constructor you copy it to an array of fixed size (`15`), or possibly uninitialized? If `a` and `this->a` are the same data, you will have troubles. – didierc Nov 05 '14 at 05:29

1 Answers1

1

Your example is working fine - just take care to delete[] with new[] allocated space accordingly after you're finished (to prevent memory leaking). Also instead of using 15 as hardcoded constant, you probably want to use the parameter size (otherwise for arrays bigger than 15 elements, you would have an memory access violation).

class ArrayClass{
public:
  ArrayClass(int* array, int size){
    a = new int[size];
    for (int i(0); i < size; i++) {
      this->a[i] = array[i];
    }
    this->size = size;
  }
  virtual ~ArrayClass(){
    delete[] a;
  }
private:
  int* a;
  int size;
};

int main(int argc, char** argv){
  int array[3] = { 1, 2, 3 };
  ArrayClass a2(array, 3);
  return 0;
}

Arrays can be allocated in C++ in different ways:

int a[3]; // will allocate the space for 3 elements statically from the stack

int* a = new int[3]; // will allocate space for 3 elements dynamically from the heap

Basically it decides in which memory your array will be located - but there are many differences in these two methods involved - see i.e. What and where are the stack and heap?. Some main differences are:

  • stack allocations can't use a dynamical length like a variable i.e. int size = 10; int a[size]; // <- is invalid
  • stack allocations are 'deleted' automatically when they are out-of-scope
  • heap allocations have to be deleted[] explicit to not leak memory

The line int* a = new int[3]; will declare a pointer-variable to an memory location where 3 int values can fit in. So after the declaration these 3 values can be addressed directly as a[0], a[1] and a[2]. After all operations are finished delete[] a; is necessary. Because if the pointer a* is getting out-of-scope (i.e. end of your function) the memory for the 3 values is not deallocated automatically.

Community
  • 1
  • 1
Constantin
  • 8,721
  • 13
  • 75
  • 126
  • Constantin: Thank you very much.But I have a doubt could you explain difference between int *a vs int* a.Is int* represent array of pointers? – user2895589 Nov 05 '14 at 14:44
  • @user2895589 I have edited my answer and explained the `int* a = new int[size];` more in detail. Hopefully it's helpful for you! – Constantin Nov 06 '14 at 03:30