MicroLIB
The C Standard Library (stdlib.h) contains a number of useful and common tools, including: - string conversion
- pseudo-random sequence generation
- dynamic memory management
- environment
- searching and sorting
- integer arithmethics
- multibyte characters
- multibyte strings
as well as some constants EXIT_FAILURE EXIT_SUCCESS MB_CUR_MAX NULL RAND_MAX and type definitions div_t ldiv_t lldiv_t size_t More information about the C standard library is available at cplusplus.com reference pages. The standard library and standard input/output (stdio.h) are implemented in libc, and this is the one library that is, by default, linked to when you compile C code; if you want use the math library, you must explicitly link it with -lm, and using the pthread library requires -lpthread, but you never need include -lc if you require something in either stdlib.h or stdio.h. The MDK-ARM that comes with μVision does not have a complete implementation of the standard library, and that library is not included by default. Instead, they provide a package called Microlib that implements many of the features in the standard library. In order to include the Microlib, it is necessary to toggle a setting in the options, which you may select by either entering Alt-F7 or selecting Project→Options for Target 'Target name' or the icon . This will bring up the Options for Target 'Target name' dialog. Select the check box next to Use MicroLIB in the Code Generation panel, as shown in Figure 1.
Figure 1. The check box selection for using the MicroLIB. Simple exampleNow you can take your previous example project and now include stdlib.h at the top and replace the body of void main(void)with int main( void ) { int i, *array; SystemInit(); while ( 1 ) { array = (int *) malloc( N * sizeof( int ) ); for ( i = 0; i < N; ++i ) { array = i; } free( array ); }}Example using a linked listThis is a rather boring example, so let's have a little more fun. In the C tutorials, there is an example that builds a singly linked list. In the source directory, you will find the header file and source file for this data structure. Save these in the same directory as your other source file for the project above. You will then need to add single_list.c to the Group Source code. You do not have to add the header file to the group, but it must still exist in the same directory as the source file. You will now replace the source file with #include <LPC17xx.h>#include <stdlib.h>#include "single_list.h"#define N 100int main( void ) { // All local variables must be declared here (C89) int r; single_list_t list; SystemInit(); while ( 1 ) { // If the list is empty, add something to it. // If the list is full (size == N), remove something from it. // Otherwise, flip a coin as to whether or not something should // be added or removed. switch( list.size ) { case 0: single_list_push_front( &list, rand() ); break; case N: single_list_pop_front( &list ); break; default: r = rand(); if ( r & 1 ) { single_list_push_front( &list, r ); } else { single_list_pop_front( &list ); } } }}What's next?Next, if you are in the laboratory, you may want to create a project that prints to the liquid-crystal dispaly (LCD): see Hello world!. Alternatively, if you want to see a real-time memory allocator where all allocations and de-allocations have a Θ(1) run time (good for Project 2), see Dynamic memory allocation.
|