Solved

in both c++ and c, how to increase the size of an array dynamically

Posted on 2003-11-02
8
393 Views
Last Modified: 2010-04-17
Hi
in c using malloc and calloc, we can assign space in heap to a data structure (e.g., array)
If during run time i want to increase the size of the array how can i do it?

realloc in c allocs new space equal to current space + additional required spce at a differenct location.
But if it cannot find

current space + additional required spce

amount of free memory which is together, it refuses to allocate new memory. though there is enough space for

additional required space

so how to get over this.


In c++ i don't know any function like realloc. I think it is possible using new itself, but i don't know how to use it. Please help clarify this to me .

Thanks
K
0
Comment
Question by:bsarvanikumar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
8 Comments
 
LVL 3

Accepted Solution

by:
guynumber5764 earned 84 total points
ID: 9667450
>>> how to get over this

Simple: all you have to do is free up more memory until the MM can join enough blocks to make a go of it.

But seriously...  There's no easy way to defrag a heap without leaving invalid pointers all over the place so you are left either increasing the size of the heap (which can be done sometimes) or rethinking your design.  For example, using a linked list instead of a array.  In some applications (such as protocol stacks)  the structure may grow several times but the finished size is known.  malloc() ing the whole thing the bat replaces the realloc()s with casting and offsets.

Generally, if malloc() or realloc() fails your prog is leaking memory somewhere and is about to die regardless: all you can do is make sure you go out with a smile.  Also, realloc() is a VERY slow call.

In C++ just use a dynamic array class (like MFC's CArray).  That'll automatically grow as required.

0
 
LVL 3

Expert Comment

by:guynumber5764
ID: 9667473
There's a whole chunk of that last post I should have proofread:

>>> rethinking your design.  For example...

If your data is highly dynamic, consider using a more dynamic data structure like a linked list.
If the maximum size is known, consider allocating the entire size right up front.  Many applications (including OSes) will pre-allocate a number of blocks like this and then manage them as a pool.  
0
 
LVL 9

Assisted Solution

by:bhagyesht
bhagyesht earned 83 total points
ID: 9668839
if in windows use CArray
0
 
LVL 6

Expert Comment

by:durgaprasad_j
ID: 9677623
In c++ , u can use Vector (std::vector) class ,  which u can use it like a linked list.

to resize that , vector
0
 
LVL 6

Assisted Solution

by:durgaprasad_j
durgaprasad_j earned 83 total points
ID: 9677626
In c++ , u can use Vector (std::vector) class ,  which u can use it like a linked list.

to resize that , vector

  Ex:
      vector <int>a;

    a.push_back(1); // automatically resizes the vector and add 1 at the end of the vector

  a.resize(10);  // now it resizes the whole vector to 10, data stored in vector will not be changed after resize .
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question