Like HowStuffWorks on Facebook!

How C Programming Works

The Importance of Memory Management in C

One of the things that makes C such a versatile language is that the programmer can scale down a program to run with a very small amount of memory. When C was first written, this was an important feature because computers weren't nearly as powerful as they are today. With the current demand for small electronics, from mobile phones to tiny medical devices, there's a renewed interest in keeping the memory requirements small for some software. C is the go-to language for most programmers who need a lot of control over memory usage.

To better understand the importance of memory management, consider how a program uses memory. When you first run a program, it loads into your computer's memory and begins to execute by sending and receiving instructions from the computer's processor. When the program needs to run a particular function, it loads that function into yet another part of memory for the duration of its run, then abandons that memory when the function is complete. Plus, each new piece of data used in the main program takes up memory for the duration of the program.

If you want more control over all this, you need dynamic storage allocation. C supports dynamic storage allocation, which is the ability to reserve memory as you need it and free that memory as soon as you're finished using it. Many programming languages have automatic memory allocation and garbage collection that handle these memory management tasks. C, though, allows (and in some cases requires) you to be explicit about memory allocation with the following key functions from the standard C library:

  • malloc -- Short for memory allocation, malloc is used to reserve a block of memory of a given size to story a certain type of data your program needs to process. When you use malloc, you're creating a pointer to the allocated memory. This isn't necessary for a single piece of data, such as one integer, which is allocated as soon as you first declare it (as in int i). However, it is an important part of creating and managing data structures such as arrays. Alternate memory allocation options in C are calloc, which also clears the memory when it's reserved, and realloc, which resizes previously reserved memory.
  • free -- Use free to force your program to free the memory previously assigned to a given pointer.

Best practice when using malloc and free is that anything you allocate should be freed. Whenever you allocate something, even in a temporary function, it remains in memory until the operating system cleans up the space. To ensure that memory is free and ready to use immediately, though, you should free it before the current function exits. This memory management means you can keep your program's memory footprint to a minimum and avoid memory leaks. A memory leak is a program flaw in which it continues using more and more memory until there's none left to allocate, causing the program to stall or crash. On the other hand, don't get so anxious about freeing memory that you free up, and thus lose, something that you need later in the same function.

Throughout this article, you've learned some of the basic structure and core concepts of the C programming language. We've looked at its history, the characteristics it has in common with other programming languages and the important features that make it a unique and versatile option for coding software. Launch over to the next page for lots more information, including some programming guides that will carry you further on your journey into C.