Solved

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

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
firstswap challenge 20 82
How can i compile this github project?? 2 89
Path to Python 9 128
Programming Language for Wordpress 7 28
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
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 …

791 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