473,325 Members | 2,308 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Learning about dynamic memory - Question

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.

Aug 9 '05 #1
1 1119

AMT2K5 wrote:
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){
that if statement is always false.
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.
allocate a new array, copy the existing elements from the old array to
the new array, then delete[] the old array.

I'd appreciate any help, thanks in advance.


Aug 10 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Akyl Tulegenov | last post by:
Dear All! I've got a question which is rather system specific case. Suppose I am dealing with a large array of ~4Gig of entities. The question is : what is faster , to address it through a...
5
by: swarsa | last post by:
Hi All, I realize this is not a Palm OS development forum, however, even though my question is about a Palm C program I'm writing, I believe the topics are relevant here. This is because I...
14
by: chai | last post by:
Can anyone help me in finding elements stored in a dynamic array in.
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
7
by: Hal Vaughan | last post by:
I have a problem with port forwarding and I have been working on it for over 2 weeks with no luck. I have found C programs that almost work and Java programs that almost work, but nothing that...
11
by: toton | last post by:
Hi, I have little confusion about static memory allocation & dynamic allocation for a cluss member. I have class like class Bar{ public: explicit Bar(){ cout<<"bar default"<<endl; }
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
10
by: swornavidhya.mahadevan | last post by:
Which allocation (Static / Dynamic) is suitable for the situation when we are trying to allocate for a overloaded memory when the memory is full and no space to allocate. What will happen if both...
19
by: smarty | last post by:
how can I find the memory allocated dynamically? is there any possibility of finding it?
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.