Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
Efficient use of dynamic memory - C and realloc

C is basically a language in which you dimension your arrays at compile time - however, at run time you can call up malloc or calloc to grab a chunk of dynamic memory. This is very useful if you don't know how much space you'll need at compile time. We've several example in our memory management module on our C training courses.

But sometimes, you'll only gradually learn how much memory you need, even at run time. Take, for example, a simple example where you want to read a file into memory and then be able to go through it, many times, line by line. You won't know how many lines there are at the beginning ... so a single malloc or calloc won't do. But there are solutions.

First, you could use realloc to extend the size of the memory block you have each time you add a new line.

Second, you could set up all your lines as linked lists, where each one contains a line of data and the address in memory of where the next line may be found.

The First option may lead to "thrashing" as memory is allocated and reallocated while the file is being read, with a large number of movements of the data between heap areas. And the second option may also be slow when you come to find data that's well down the list.

There is a Third Way. You can use realloc, but extend the size of the memory blocks in much more substantial chunks, and that's something that we did on today's course. The full source code may be found here. The extra lines / critical lines are:
  need = counter + 1;
  if (have < need) {
    info = realloc(info,(counter+16) * sizeof(char *));
    have = counter+16;
  }

Basically saying "if you haven't got enough space for another line, then grab space for 16 more ...

(written 2010-03-10)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
C050 - C and C based languages - C - General
  [3129] Extra courses - C and C++ - (2011-01-12)
  [3053] Make - automating the commands for building and installing - (2010-11-16)
  [2848] C course - final course example puts it all together - (2010-07-02)
  [2763] Our C and C++ training course are on Open Source platforms - (2010-05-13)
  [2536] All the Cs ... and Java too - (2009-12-13)
  [2504] Learning to program in ... - (2009-11-15)
  [2091] C, C++ and C# ... Java and JavaScript - (2009-03-20)
  [2086] C Programming v Learning to Program in C. Which course? - (2009-03-17)
  [2002] New C Examples - pointers, realloc, structs and more - (2009-01-20)

C212 - C and C based languages - Memory Management
  [4128] Allocating memory dynamically in a static language like C - (2013-06-30)
  [3416] Storing Tcl source code encoded, and running via your own C program - (2011-09-02)
  [3386] Adding the pieces together to make a complete language - C - (2011-08-11)
  [3144] Setting up arrays in C - fixed size at compile time, or dynamic - (2011-01-24)
  [3118] Arrays of arrays - or 2D arrays. How to program tables. - (2011-01-02)
  [1845] Passing a table from Lua into C - (2008-10-18)
  [1670] Dynamic Memory Allocation in C - (2008-06-09)
  [1589] Dynamic Memory Allocation in C - calloc, realloc - (2008-03-22)
  [1581] What is an lvalue? (Perl, C) - (2008-03-18)
  [1497] Training Season Starts again! - (2008-01-07)


Back to
Is it worth it?
Previous and next
or
Horse's mouth home
Forward to
Pointers to Pointers to Pointers - what is the point?
Some other Articles
Multiple Inheritance in C++ - a complete example
Dear Planners, please provide viable alternatives
Melksham - Carnival, Party in the Park, and Spot the Oddity
Pointers to Pointers to Pointers - what is the point?
Efficient use of dynamic memory - C and realloc
Is it worth it?
Web page to telephone calls / links using an iPhone
Random thoughts on Melksham Town Planning and development
Early Spring walk in Melksham
Oliver Cromwell at Bristol Temple Meads
4300 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2669_Eff ... alloc.html • PAGE BUILT: Thu Sep 18 15:30:25 2014 • BUILD SYSTEM: WomanWithCat