Solved

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

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This is an explanation of a simple data model to help parse a JSON feed
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.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

760 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

18 Experts available now in Live!

Get 1:1 Help Now