| | |
help with array resizing
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Dec 2008
Posts: 2
Reputation:
Solved Threads: 0
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:
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.
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:
C++ Syntax (Toggle Plain Text)
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.
int* a parameter treated as a local variable in the function body. See what happens when you call the function: c++ Syntax (Toggle Plain Text)
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!
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.
•
•
Join Date: Dec 2008
Posts: 2
Reputation:
Solved Threads: 0
•
•
•
•
int* aparameter treated as a local variable in the function body. See what happens when you call the function:
You can pass parr by reference too (declare parameter a asc++ Syntax (Toggle Plain Text)
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!int*& aor pass a pointer to parr (declare parameter a asint** aand use*a = tempto change parr value.
Much appreciated, thank you very much and you can mark this as solved.
Hails.
Joe
![]() |
Similar Threads
- Resizing of an array. (C++)
- Resizing output window (Java)
- How to load a JPEG image file, store it in array and then save it (Visual Basic 4 / 5 / 6)
- Problem in Reading Array Variable (C)
- Made my own vector thingy (C)
Other Threads in the C++ Forum
- Previous Thread: Urgent solution needed.....
- Next Thread: file polygon.h
Views: 370 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






