A
AMT2K5
Hello folks. I am at the chapter in my textbook towards learning about
dynamic memory and am practicing with a few examples and lessons from a
variety of places.
I am confused about one certain function within my practice question,
it reads
"int grow(int s = 10) - a private function that allocates memory to
savings. If savings is NULL allocate enough memory to hold s Accounts.
If savings is not NULL increase by s the size of the array whose
address is stored in savings, do not lose any of the data currently
stored in the array. If the memory allocation succeeds set arraySize to
the size of the array, other wise set arraySize and size to zero.
Return arraySize."
Trying to interpret that code, I have...
int grow(int s = 10) {
// Should I have delete [] savings here?
if(savings = NULL){
savings = new Account [ s ]; }
else{ savings = new Account [ arraySize + s];
// "Whose address is stored in savings? How do I do that?
}
Basically im confused on how to not lose any data by growing the size
of the array.
I'd appreciate any help, thanks in advance.
dynamic memory and am practicing with a few examples and lessons from a
variety of places.
I am confused about one certain function within my practice question,
it reads
"int grow(int s = 10) - a private function that allocates memory to
savings. If savings is NULL allocate enough memory to hold s Accounts.
If savings is not NULL increase by s the size of the array whose
address is stored in savings, do not lose any of the data currently
stored in the array. If the memory allocation succeeds set arraySize to
the size of the array, other wise set arraySize and size to zero.
Return arraySize."
Trying to interpret that code, I have...
int grow(int s = 10) {
// Should I have delete [] savings here?
if(savings = NULL){
savings = new Account [ s ]; }
else{ savings = new Account [ arraySize + s];
// "Whose address is stored in savings? How do I do that?
}
Basically im confused on how to not lose any data by growing the size
of the array.
I'd appreciate any help, thanks in advance.