Solved

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

Posted on 2003-11-02
8
390 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
  • 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
This is about my first experience with programming Arduino.
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 …

831 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