Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This is an explanation of a simple data model to help parse a JSON feed
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
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 …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

932 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now