Standard C heap initialization, rvds4.0 + 6410 initialize C heap malloc

Source: Internet
Author: User

Standard C heap initialization, rvds4.0 + 6410 initialize C heap malloc

I tried to use the method of initializing the heap through Assembly many times before. It can be used on Keil, but it has never been successful on rvds4.0. Today I found a method to initialize the heap directly in C.

First, the Assembly entry is not processed.

; Skip; jump to the C main function; --------------------------------------------------------------------- importmain blmainb.

Then add the header file and compiler instruction to the C program, and implement a function at the same time.

// Standard C heap support # include "rt_heap.h" # pragma import (_ use_realtime_heap) // This function is declared in rt_heap.h and needs to be implemented by the user, returns any unsigned _ rt_heap_extend (unsigned size, void ** block) {return 0 ;}

Finally, initialize the heap space after entering the main function. I used ok6410 to set the last MB of memory to the heap space.

_ Init_alloc (0x56400000, 0x600000008-8); // initialize the heap range.

The above completes the heap initialization. Now you can use malloc. Of course, do not forget to release it.

In rvds4.0, I also found a simple description, which is concise enough.

Finally, let's test it.

// Request Heap space p1 = (u8 *) malloc (1024); If (INT) P1 = 0) {uart_printf ("malloc error! \ R \ n ");} else {uart_printf (" malloc OK = 0x % 08x \ r \ n ", P1 );} // The second Request Heap space P2 = (u8 *) malloc (1024); If (INT) P2 = 0) {uart_printf ("malloc error! \ R \ n ");} else {uart_printf (" malloc OK = 0x % 08x \ r \ n ", P2 );} // release the heap space free (P1) for the first request; // The Third Request Heap space P3 = (u8 *) malloc (1024); If (INT) p3 = 0) {uart_printf ("malloc error! \ R \ n ");} else {uart_printf (" malloc OK = 0x % 08x \ r \ n ", P3 );}

Result

The result shows that the requested heap is within the defined range. After the first heap space is released, it is applied again for the third time.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.