473,612 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1132

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
1627
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 binary file created on disk or using dynamic memory which is extended through a large swap file (again on disk)? Note that swap file is created at the point of the disk partition. I would be grateful if in addition to your comments you could point me...
5
3746
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 believe the problem centers around my handling of strings, arrays, pointers and dynamic memory allocation. Here is the problem I'm trying to solve: I want to fill a list box with a list of Project Names from a database (in Palm this is more...
14
476
by: chai | last post by:
Can anyone help me in finding elements stored in a dynamic array in.
1
9622
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 and I was wondering if anyone here would be able to give me some tips for young players such as myself, for learning the language. Is this the best Newsgroup for support with JAVA?
7
2377
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 does what I need. I've even tried writing a port forwarder in Java and found problems that nobody seems to have the answer to in forums. I need to make it work essentially the same on both Windows and Linux. There is one program, in C, that...
11
3037
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
7959
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 compiled and run without any erros but the second program has a run time error when the function return from allocate and the ptr become NULL. How to fixed this? Second Program: /* Best Method to allocate memory for 2D Array because it's ...
10
4410
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 the allocation is impossible. Sworna vidhya
19
1682
by: smarty | last post by:
how can I find the memory allocated dynamically? is there any possibility of finding it?
0
8162
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8605
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8565
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8246
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8415
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7039
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6076
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2550
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1413
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.