Keil中使用MicroLib,以及malloc

54 篇文章 3 订阅

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 example

Now 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] = i;
		}

		free( array );
	}
}

Example using a linked list

This 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 100

int 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.

转自: http://blog.csdn.net/cenzmin/article/details/52169763
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值