help with array resizing

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Dec 2008
Posts: 2
Reputation: gm999 is an unknown quantity at this point 
Solved Threads: 0
gm999 gm999 is offline Offline
Newbie Poster

help with array resizing

 
0
  #1
Dec 5th, 2008
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[size];
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:

  1. void newInt(int value, int * a, int & size){
  2. int newsize = size + 1;
  3. int * temp = new int[newsize];
  4. //move over values to temporary array
  5. for(int i=0; i < size; i++){
  6. temp[i] = a[i];
  7. }
  8. //insert the new value in the temporary array
  9. temp[size] = value;
  10. //free memory in old array
  11. delete [] a;
  12. //get old array to reference the temporary one
  13. a = temp;
  14. //increase the size of a to keep track of changes
  15. size++;
  16. }

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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: help with array resizing

 
0
  #2
Dec 5th, 2008
int* a parameter treated as a local variable in the function body. See what happens when you call the function:
  1. size_t n = 2008;
  2. int* parr = new int[n];
  3. // parr points to a new memory chunck of 2008 integers.
  4. ...
  5. newInt(2008,parr,n);
  6. // pointer parr value initializes local a parameter of newInt
  7. // (pass parameter by value)
  8. // n passed by reference so it's possible to change it.
  9. ... function body works with copy of parr
  10. // parr has the same value as before newInt call,
  11. // 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.
Last edited by ArkM; Dec 5th, 2008 at 5:20 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 2
Reputation: gm999 is an unknown quantity at this point 
Solved Threads: 0
gm999 gm999 is offline Offline
Newbie Poster

Re: help with array resizing

 
0
  #3
Dec 5th, 2008
Originally Posted by ArkM View Post
int* a parameter treated as a local variable in the function body. See what happens when you call the function:
  1. size_t n = 2008;
  2. int* parr = new int[n];
  3. // parr points to a new memory chunck of 2008 integers.
  4. ...
  5. newInt(2008,parr,n);
  6. // pointer parr value initializes local a parameter of newInt
  7. // (pass parameter by value)
  8. // n passed by reference so it's possible to change it.
  9. ... function body works with copy of parr
  10. // parr has the same value as before newInt call,
  11. // 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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 370 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC