Increasing the size of a pointer array.

Hello, I'm a beginner in a second level class doing an assignment about a bank account. I have to write a virtual function that prints a statement as a list of transactions. I'm pretty sure that because the assignment is part of the chapter that talks about dynamic allocation, pointer arrays, and polymorphism, I have to store the list of transactions in a pointer array that can expand.

But I've searched many places and I'm not even sure anymore that a pointer array can expand.

Must I do a new "<identifier> = new <datatype>[arraySize + 1]" and copy the array over, or am I way off?
Last edited on
Are you needing an array of pointers, or a pointer to an array?
Last edited on
What do you mean with a pointer array? int *array, or int **array. Both can be expanded at run time, you just need to allocate more memory for them:
1
2
3
4
5
6
7
int *arr = new int[10];
delete[] arr; //Must use delete to avoid memory leak.
int *arr = new int[20];
//Same for **:
int **arr = new int*[10];
delete[] arr;
int **arr = new int*[20];

Of course here you are immediately deleting your old data. In a function to expand the array you would need a temporary pointer which points to either the new or the old array.
You can always just use the std::vector class if that's allowed, instead of basically building your own.
This is ugly and probably not what you want but...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

#include <iostream>
#include <cstring>

int main()
{
  char *text = 0;
  char *newtext = 0;
  text = new char[8];
  strcpy(text,"1234567");
  std::cout << "text = " << text << std::endl;
  newtext = new char[10];
  strcpy(newtext,text);
  strcat(newtext,"89");
  delete [] text;
  text = newtext;
  std::cout << "text = " << text << std::endl;
  delete [] newtext;
  return 0;
}
Your most likely taking about a dynamic array which is an array that you reference by using a dynamic pointer. All arrays are technically referenced by a pointer but it's usually a static pointer that you cannot directly manipulate.

So if you wanted a dynamic array of ints:
 
int* dynamicArray = new int[20];


If you fill this array with 20 ints and decide you want to expand you will need to copy it into a larger array:

1
2
3
4
int* largerArray = new int[30];

for (int i = 0; i < 20; i++)
     largerArray[i] = dynamicArray[i];



Now delete the original array:

delete [ ] dynamicArray;


If you wanted to make things easier you could use a vector which function just like an array but will automatically expand for you. (it does the copy process behind the scenes)

EDIT - I think for the sake of convenience your best bet is create struct or class for you transactions and store them in a vector. Even better would be to store them in a map with a transaction number as the key. Then you could implement a search function rather easily.

You could just as easily implement it wuth a vector but you would have to actually search through them all manually.


Here is what I might start with in my transaction class:

int GetTransactionNumber() const;
Print(ostream& out) const;
bool UpdateSale(int sale);

int id;
(class) CustomerInfo; (this would store all their info separately)
double sale;
string item;

and so on....These methods will come in handy and let you encapsulate the data of each transaction.



Last edited on
Hey, guys. Thanks a lot. I ended up doing something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void bankAccount::increaseStatementSize(int size){

	statementSize = size;
	transaction * oldArray = pointerTransaction;
	pointerTransaction = new transaction[statementSize];
	
	for (int loop2 = 0; loop2 <= statementSize - 2; loop2++){ //copy info from old array to new
		pointerTransaction[loop2].balance = oldArray[loop2].balance;
		pointerTransaction[loop2].shiftAmount = oldArray[loop2].shiftAmount;
		pointerTransaction[loop2].transTypeName = oldArray[loop2].transTypeName; 
	}

	delete [] oldArray;	//delete temp array
}


I store the array just fine, now. Thanks a ton for your help. :>
I don't quite understand this: loop2 <= statementSize - 2. Why minus 2?

If size is more than 1 more than the old size, then you're program will crash. This is because Lines 8-10 try to reference indices that may or may not exist. I think better to move line 3 to line 12, and then make line 5 be pointerTransaction = new transaction[size];.

And you run big risks with size being an int, make it and statementSize unsigned.

You may even consider making increaseStatementSize not accept any variables, and to just increase the size by one.
Last edited on
Topic archived. No new replies allowed.