Embedded Systems Design Europe - June/July 2008 - (Page 19) memory Listing 1 Static memory allocation in C. // // static memory allocation void alloc_static() { static int array[10]; // static allocation int n; // // populate “array” for (n=0;n<10;n++) { array[n]=n+1; printf(“%d:%d\n”,n,array[n]); } Listing 2 Stack allocation in C. // // stack allocation void alloc_stack() { int array[10]; // stack allocation int n; // // populate “array” for (n=0;n<10;n++) { array[n]=n+1; printf(“%d:%d\n”,n,array[n]); } } Listing 3 Heap allocation in C. // // heap allocation void alloc_heap() { int* array; int n; if (needMemory) // allocate memory only if needed { // // allocate array on heap array = (int *)malloc(10*sizeof(int)); free(array); } } // free memory works even on processors without stack support. However, this method inefficiently allocates all storage memory for the lifetime of the program, so more memory is allocated than is actually in use. In addition, the amount of memory is fixed and must be specified at compile time. For example in Listing 1, the memory for variable “array” is allocated statically. In stack allocation, the processor allocates a fixed amount of memory when a function is invoked. The memory for the currently executing function is maintained on a stack, and when the function exits, the memory on the stack is released. This method uses memory more efficiently than static allocation, because memory is allocated and released as needed by each function. However, the stack is limited to a finite area of memory. It can be difficult to predict how large the overall stack needs to be, and stack overflow can be difficult to detect. Like static allocation, the size of variables on the stack must be specified at compile time. Listing 2 illustrates stack allocation for variable “array.” Finally, in dynamic allocation, you manually allocate memory at run time. The memory is drawn from the heap, using C’s malloc and free commands. Because you have complete control over memory usage, dynamic allocation allows more efficient use of memory than either static or stack allocation. Listing 3 shows an example of dynamic allocation. The flexibility of dynamic memory allocation, however, comes at a price: • • • • In C, the developer has the choice of allocating memory either on the stack, in static memory, or dynamically (on the heap). Each allocation method has its advantages and disadvantages. Static allocation, in which the compiler sets aside a fixed amount for each variable, is the simplest method and Manual memory management is error prone, and heap-related software problems, such as memory leaks, are particularly difficult to debug. Searching the heap for an appropriate size block takes an indeterminate amount of time, which is unacceptable for “hard” real-time systems. Repeatedly allocating and freeing memory blocks of different sizes eventually leads to fragmentation. Custom memory allocators, such 19 www.embedded.com/europe | embedded systems design europe | JUNE – JULY 2008 http://www.embedded.com/europe
Table of Contents Feed for the Digital Edition of Embedded Systems Design Europe - June/July 2008 Embedded Systems Design Europe - June 2008 Contents Work in Progress to Define Compact PCI Plus Power.org Demonstrates New Tools Project Supports Multi-core System Programming Altium Links Electronic to Mechanical Design PLDs Look to Cut Power Budget and Costs Project to Provide Coverage Analysis Tool Microsoft Details Windows Embedded Update Cover Feature: Leveraging Virtual Hardware Platforms for Software Allocating Memory in MATLAB-to-C Code MDD & IDEs: Making the Twain Meet in Embedded System Designs Debugging Mixed Signal Designs for Infrequent & Random Events Why Open Source is the Natural Choice for High-security Systems Bringing the Benefits of Low Power CPUs to Modular Design New Products Advertising Contacts Embedded Systems Design Europe - June/July 2008 Embedded Systems Design Europe - June/July 2008 - Embedded Systems Design Europe - June 2008 (Page 1) Embedded Systems Design Europe - June/July 2008 - Embedded Systems Design Europe - June 2008 (Page 2) Embedded Systems Design Europe - June/July 2008 - Contents (Page 3) Embedded Systems Design Europe - June/July 2008 - Contents (Page 4) Embedded Systems Design Europe - June/July 2008 - Contents (Page 5) Embedded Systems Design Europe - June/July 2008 - Project Supports Multi-core System Programming (Page 6) Embedded Systems Design Europe - June/July 2008 - Project Supports Multi-core System Programming (Page 7) Embedded Systems Design Europe - June/July 2008 - Altium Links Electronic to Mechanical Design (Page 8) Embedded Systems Design Europe - June/July 2008 - Altium Links Electronic to Mechanical Design (Page 9) Embedded Systems Design Europe - June/July 2008 - PLDs Look to Cut Power Budget and Costs (Page 10) Embedded Systems Design Europe - June/July 2008 - PLDs Look to Cut Power Budget and Costs (Page 11) Embedded Systems Design Europe - June/July 2008 - Microsoft Details Windows Embedded Update (Page 12) Embedded Systems Design Europe - June/July 2008 - Microsoft Details Windows Embedded Update (Page 13) Embedded Systems Design Europe - June/July 2008 - Cover Feature: Leveraging Virtual Hardware Platforms for Software (Page 14) Embedded Systems Design Europe - June/July 2008 - Cover Feature: Leveraging Virtual Hardware Platforms for Software (Page 15) Embedded Systems Design Europe - June/July 2008 - Cover Feature: Leveraging Virtual Hardware Platforms for Software (Page 16) Embedded Systems Design Europe - June/July 2008 - Cover Feature: Leveraging Virtual Hardware Platforms for Software (Page 17) Embedded Systems Design Europe - June/July 2008 - Allocating Memory in MATLAB-to-C Code (Page 18) Embedded Systems Design Europe - June/July 2008 - Allocating Memory in MATLAB-to-C Code (Page 19) Embedded Systems Design Europe - June/July 2008 - Allocating Memory in MATLAB-to-C Code (Page 20) Embedded Systems Design Europe - June/July 2008 - Allocating Memory in MATLAB-to-C Code (Page 21) Embedded Systems Design Europe - June/July 2008 - MDD & IDEs: Making the Twain Meet in Embedded System Designs (Page 22) Embedded Systems Design Europe - June/July 2008 - MDD & IDEs: Making the Twain Meet in Embedded System Designs (Page 23) Embedded Systems Design Europe - June/July 2008 - MDD & IDEs: Making the Twain Meet in Embedded System Designs (Page 24) Embedded Systems Design Europe - June/July 2008 - MDD & IDEs: Making the Twain Meet in Embedded System Designs (Page 25) Embedded Systems Design Europe - June/July 2008 - MDD & IDEs: Making the Twain Meet in Embedded System Designs (Page 26) Embedded Systems Design Europe - June/July 2008 - Debugging Mixed Signal Designs for Infrequent & Random Events (Page 27) Embedded Systems Design Europe - June/July 2008 - Debugging Mixed Signal Designs for Infrequent & Random Events (Page 28) Embedded Systems Design Europe - June/July 2008 - Debugging Mixed Signal Designs for Infrequent & Random Events (Page 29) Embedded Systems Design Europe - June/July 2008 - Debugging Mixed Signal Designs for Infrequent & Random Events (Page 30) Embedded Systems Design Europe - June/July 2008 - Why Open Source is the Natural Choice for High-security Systems (Page 31) Embedded Systems Design Europe - June/July 2008 - Why Open Source is the Natural Choice for High-security Systems (Page 32) Embedded Systems Design Europe - June/July 2008 - Why Open Source is the Natural Choice for High-security Systems (Page 33) Embedded Systems Design Europe - June/July 2008 - Bringing the Benefits of Low Power CPUs to Modular Design (Page 34) Embedded Systems Design Europe - June/July 2008 - Bringing the Benefits of Low Power CPUs to Modular Design (Page 35) Embedded Systems Design Europe - June/July 2008 - Bringing the Benefits of Low Power CPUs to Modular Design (Page 36) Embedded Systems Design Europe - June/July 2008 - New Products (Page 37) Embedded Systems Design Europe - June/July 2008 - New Products (Page 38) Embedded Systems Design Europe - June/July 2008 - New Products (Page 39) Embedded Systems Design Europe - June/July 2008 - New Products (Page 40) Embedded Systems Design Europe - June/July 2008 - New Products (Page 41) Embedded Systems Design Europe - June/July 2008 - New Products (Page 42) Embedded Systems Design Europe - June/July 2008 - Advertising Contacts (Page 43) Embedded Systems Design Europe - June/July 2008 - Advertising Contacts (Page 44)
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.