For 2023 - we are now fully retired from IT training. We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.
Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!
I am also active in many other area and still look after a lot of web sites - you can find an index ((here)) |
C and C based languages module C212
Memory Management
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 and C++, Programming in C, C and C++ Programming, Learning to program in C and 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?
Articles and tips on this subject | updated | 4635 | Encapsulating logic in functions and structs - the C approach to Object Oriented techniques Although C is not what is described as an "Object Oriented Language", many of the good programming principles that are applied naturally in OO programming can be applied in C too.
I wrote an example that showed this yesterday - with a C struct defining what is in essence an object, and being referenced ... | 2016-01-31 | 4634 | Regression testing - via a very short C testing framework Code testing is important. Separation of code into separate, managable sections is important. Retesting of code running all the old tests is important when the code sections are enhanced / debugged.
At the end of this week's C course, we wrote a whole series of pieces of logic relating to web server ... | 2016-01-30 | 4340 | Simple C structs - building up to full, dynamic example During the final day of our "Learning to program in C" course - today - we built up an example that introduced structs, then added dynamic memory allocation so that we ended up with a flexible program capable of taking an unlimited amount of data. The build up examples also show (in the last program) ... | 2014-12-03 | 4128 | Allocating memory dynamically in a static language like C From last weeks' C course - a complete example showing dynamic memory allocation - reading in any file of data, lines of varying length, and packing them in neat. Source code [here].
There's an issue with reading data of unknown size into a fixed declaration language like C... allocation stack memory ... | 2013-07-15 | 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
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 [here].
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
Ruby,
Lua,
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.
|