Hi guys, I'm quite new to C++ and I'm having problems with a simple operation.
I have to resize an array of structs, but seeing as I am having a problem with the concept, i'll post an example with a simple array of integers.

Please bear in mind that this is for an assignment in college, therefore - while using vectors would solve my problem, - I'm not allowed use anything other than simple arrays.

Presume that in main() i have:

int size = 3;
int * a = new int;
a[0] = 1;
a[1] = 2;
a[2] = 3;

i want to write a function to push a new value into the array a. I thought of doing it like this:


void newInt(int value, int * a, int & size){
  int newsize = size + 1;
  int * temp = new int[newsize];
  //move over values to temporary array
  for(int i=0; i < size; i++){
    temp[i] = a[i];
  }
  //insert the new value in the temporary array
  temp[size] = value;
  //free memory in old array
  delete [] a;
  //get old array to reference the temporary one
  a = temp;
  //increase the size of a to keep track of changes
  size++;
}

well, if i run this it apparently works the first time but if i print out the values contained in a i get something like:

0
2
3
4007152

when i obviously expected

1
2
3
<whatever new value had been passed to newInt()>


Thanks everybody for your help, hope the problem is clear enough.

Recommended Answers

All 2 Replies

int* a parameter treated as a local variable in the function body. See what happens when you call the function:

size_t n = 2008;
int* parr = new int[n]; 
// parr points to a new memory chunck of 2008 integers.
...
newInt(2008,parr,n); 
// pointer parr value initializes local a parameter of newInt
// (pass parameter by value)
// n passed by reference so it's possible to change it.
... function body works with copy of parr
// parr has the same value as before newInt call,
// but now it points to deallocated (by newInt) memory!

You can pass parr by reference too (declare parameter a as int*& a or pass a pointer to parr (declare parameter a as int** a and use *a = temp to change parr value.

int* a parameter treated as a local variable in the function body. See what happens when you call the function:

size_t n = 2008;
int* parr = new int[n]; 
// parr points to a new memory chunck of 2008 integers.
...
newInt(2008,parr,n); 
// pointer parr value initializes local a parameter of newInt
// (pass parameter by value)
// n passed by reference so it's possible to change it.
... function body works with copy of parr
// parr has the same value as before newInt call,
// but now it points to deallocated (by newInt) memory!

You can pass parr by reference too (declare parameter a as int*& a or pass a pointer to parr (declare parameter a as int** a and use *a = temp to change parr value.

Worked like a charm, I suppose i'll have to start by reviewing variable scope in c++.

Much appreciated, thank you very much and you can mark this as solved.

Hails.
Joe

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.