Exercises, examples and other material relating to training module C212. This topic is presented on public courses
Learning to Program in C,
Learning to program in C++,
Programming in C,
C and C++ Programming
Background
When you declare an array in C, it occupies a fixed amount
of memory and that's fixed at compile time. What if you don't
know how much memory you'll need? One possible solutions (there
are others in C++) is to use the suppllied "malloc" familiy of
memory management functions that allow you to use dynamic memory
allocation - you can decide how big your arrays need to be as
your program runs, and even delete them and re-define them to
be bigger if you need to.
Related technical and longer articles
Want something more dynamic than an array?
327b
| Articles and tips on this subject | updated |
| 3416 | Storing Tcl source code encoded, and running via your own C program Tcl is a language. And Tcl is also a C library. How's that?
Tcl originates from a requirement to provide a taioring capability within C programs that could be accessed in a program-like style. And the program-like style that was implemented actually had the full capabilities of a programming language. ... | 2011-09-02 |
| 3386 | Adding the pieces together to make a complete language - C Once we've covered the fundamentals of programming on a C course, we move on to cover pointers, arrays, structures, strings, input/output, and dynamic memory allocation. We teach and illustrate each of them, and we have our delegates write practical exercises to make sure that they have a grasp of ... | 2011-08-11 |
| 3144 | Setting up arrays in C - fixed size at compile time, or dynamic You can store a whole series of data values of the same type in an array in C.
The easiest way to declare an array is something like:
int history[20];
and that will give you an array of 20 elements - which you can refer to as elements number 0 to 19, so:
history[4] = 1234;
to ... | 2011-01-25 |
| 2848 | C course - final course example puts it all together "Yes - but how do we put all of those things together?" ... delegate question on many programming courses. As a trainer, I have to take our students through each of the main features of a language - to ensure they understand the fundamentals and perhaps more. But the it's just as important to take ... | 2011-01-05 |
| 3118 | Arrays of arrays - or 2D arrays. How to program tables. It's shorthand when you're learning a new programming language to ask "how does it handle multiple dimension arrays" ... but in practise very few languages actually support true multidimensional arrays these days, and in those which do you might be well advised to use an alternative that's more flexible.
What ... | 2011-01-03 |
| 1845 | Passing a table from Lua into C In a previous article, I showed you a simple example of how you can call a function that's written in C from Lua, and a second example that extended that - passing simple parameters in to the C function, and returning results.
But it's often more complex that that - for example, you may with to pass ... | 2010-06-19 |
| 2669 | 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 ... | 2010-03-10 |
| 1670 | Dynamic Memory Allocation in C In C, you give fixes sizes for arrays at compile time .... which means you either have to know ahead of time how big something will be, or allow enough space for the worst case scenario.
Rather than using arrays, you can use pointers to the memory heap - and in this way you can use functions to give ... | 2008-06-10 |
| 1589 | Dynamic Memory Allocation in C - calloc, realloc The Problem
The C programming language is a fixed array size language - you write the size of your variables into your code and that's how big they are. Overrun an array and you corrupt other memory - and it's up to you the programmer to ensure this doesn't happen. Many past security leaks, such as ... | 2008-03-22 |
| 1581 | What is an lvalue? (Perl, C) An lvalue is an expression that you can write on the left hand side of an assignment statement - in other words an expression that defines a specific memory address of a variable.
The most common lvalues are simple variables or array / list / hash / dictionary members ... for example
$hello = "Hello ... | 2008-03-18 |
| 1497 | Training Season Starts again! I'm glad that our business isn't heavily seasonal - tourist places like Longleat have a heavy summer and a barren winter, and The Christmas Shop must, surely, be heading for a quiet few months.
But we are - slightly - seasonal; we have a quiet three weeks where no-one wants to learn Perl or PHP (or ... | 2008-01-07 |
Examples from our training material
| afp2.c | Dymanic array - runtime sizing on using calloc |
| loglook.c | Complete example - regex, realloc, string and file handling, etc |
| slurpit.c | Dynamic memory allocation - reading a file of unknown length |
| strainer.c | reallocing for an array of structures |
| supper.c | realloc - resizing storage dynamically |
| twelve.c | Dynamically sized collection |
Background information
Some modules are
available for download as a sample of our material or under an
Open Training Notes License for free download from
http://www.training-notes.co.uk.
Topics covered in this module
Malloc calloc realloc free and cfree.
Using Pointer arithmetic.
Creating variable length arrays and strings.
Complete learning
If you are looking for a complete course and not just a information on a single subject, visit our
Listing and schedule page.
Well House Consultants specialise in training courses in
Python,
Perl,
PHP, and
MySQL. We run
Private Courses throughout the UK (and beyond for longer courses), and
Public Courses at our training centre in Melksham, Wiltshire, England.
It's surprisingly cost effective to come on our public courses -
even if
you live in a different
country or continent to us.
We have a technical library of over 700 books on the subjects on which we teach.
These books are available for reference at our training centre. Also
available is the Opentalk
Forum for discussion of technical questions.