![]() |
| | #1 |
| HelpingYouHelpUsHelpUsAll Join Date: Dec 2007 Location: In your nightmares
Posts: 223
| Dynamic Array Allocation function Code: #include <iostream>
using namespace std;
void read(int *a, int &n);
// read the first n elements of array a
int SIZE = 5;
int main()
{
int *list1 = new int[SIZE];
int *list2 = new int[SIZE];
int noeles1=0, noeles2, test1, test2;
read(list1, noeles1);
read(list2, noeles2);
cout << "List 1: " << endl;
for (test1=0; test1<noeles1; test1++) {
cout << list1[test1];
cout << " ";
}
cout << "\nList 2: " << endl;
for (test2=0; test2<noeles2; test2++) {
cout << list2[test2];
cout << " ";
}
return 0;
}
// write the body of following functions
void read(int *a, int &n)
{
SIZE = 5;
int k;
cout << "Enter values:" << endl;
n=0;
do {
if (n<SIZE) cin >> a[n];
else {
SIZE *= 2; //double array
int *temp = new int[SIZE];
for (k = 0; k<SIZE; k++) temp[k] = a[k];
delete [] a;
a = temp;
cin >> a[n];
}
} while (a[n++] != -1);
n--;
}
__________________ long time no C; //seige You miss 100% of the people you don't C; Code: if (language != LANG_C && language != LANG_CPP)
drown(language);
Last edited by P4R4N01D; 05-15-2009 at 12:30 AM. Reason: remove some debugging stuff |
| P4R4N01D is offline | |
| | #2 |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,338
| It looks like you forgot to pass the pointer by reference, so the assignment to a in read() merely changes the local pointer.
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way |
| laserlight is offline | |
| | #3 |
| HelpingYouHelpUsHelpUsAll Join Date: Dec 2007 Location: In your nightmares
Posts: 223
| Thanks for the quick reply, I thought passing a pointer to int was passing by reference. Would changing the function to void read(int **a, int &n) pass by reference (as int &*a didn't work and int a would just be an int). If I change a to be passed as above & change every occurance of a to *a I get a runtime error: "the memory couldn't be written".
__________________ long time no C; //seige You miss 100% of the people you don't C; Code: if (language != LANG_C && language != LANG_CPP)
drown(language);
Last edited by P4R4N01D; 05-15-2009 at 12:44 AM. Reason: read -> written |
| P4R4N01D is offline | |
| | #4 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,338
| Quote:
Code: void read(int *&a, int &n) Code: void read(int*& a, int& n)
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is offline | |
| | #5 | |
| Algorithm Dissector Join Date: Dec 2005 Location: New Zealand
Posts: 2,733
| Quote:
Types are read right to left (well there's more to it than that when you get more complex examples, but that's true enough for you, for now).
__________________ My homepage Advice: Take only as directed - If symptoms persist, please see your debugger | |
| iMalc is offline | |
| | #6 |
| HelpingYouHelpUsHelpUsAll Join Date: Dec 2007 Location: In your nightmares
Posts: 223
| As I have not learnt about vectors yet, I didn't even think about using them. Breifly looking at a reference on them I can see how they would be useful as an alternate method. Another reason why I used this approach was to modify an existing program that read in a fixed number of integers. Thanks for your help.
__________________ long time no C; //seige You miss 100% of the people you don't C; Code: if (language != LANG_C && language != LANG_CPP)
drown(language);
|
| P4R4N01D is offline | |
| | #7 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Can I also suggest that you modify the Code: if (n<SIZE) cin >> a[n];
else {
SIZE *= 2; //double array
int *temp = new int[SIZE];
for (k = 0; k<SIZE; k++) temp[k] = a[k];
delete [] a;
a = temp;
cin >> a[n];
}
Also, you should not copy the NEW size from a[k], as that is outside the size of the original allocation, so you probably need another variable to hold the original (or new) size until you have updated the content. Finally, I would suggest that you do not use a global variable for size - there is a bug in your current code because of the size for the frist array being changed, so that if you enter 100 values in the first set, the second set will overwrite the end of your allocation after the 5 first elements have been entered. You need to keep track of the size of each array, not have one variable for both! -- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. |
| matsp is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| pointer to function and dynamic allocation | cfdprogrammer | C Programming | 7 | 07-08-2009 10:36 AM |
| sorting number | Leslie | C Programming | 8 | 05-20-2009 04:23 AM |
| pointer to array with dynamic allocation | cfdprogrammer | C Programming | 22 | 04-07-2009 09:56 AM |
| dynamic allocation of 2d array | owi_just | C Programming | 4 | 05-09-2005 12:50 AM |
| c++ linking problem for x11 | kron | Linux Programming | 1 | 11-19-2004 10:18 AM |