Embedded Systems Design - June 2008 - (Page 34) feature software projects forbid the use of dynamic memory allocation entirely. In MATLAB, you don’t need to think about memory allocation. The MATLAB interpreter handles details of array memory and data types, leaving the algorithm developer free to concentrate on the algorithm itself. In fact, typical MATLAB users don’t even worry about array sizes, as MATLAB will automatically resize arrays as required. For this article, however, we’ll identify two types of MATLAB arrays: fixed-size arrays and variable-size arrays. Fixed-size arrays are MATLAB arrays for which the dimensions remain fixed during run time. Variable-size arrays change in size or shape during run time. Some MATLAB examples are shown in Listing 4. Variable-size arrays are an extremely useful feature of MATLAB, and most non-trivial MATLAB programs make use of them; sometimes you simply don’t know how large an array (such as an image or resampled buffer) will be until run time. Although the MATLAB language doesn’t explicitly distinguish between fixedsize arrays and variable-size arrays, you’ll see that this distinction becomes critical when translating to C. EMBEDDED C FOR FIXED-SIZE ARRAYS When translating MATLAB code to C, you must choose how to allocate memory for your data. If the MATLAB code uses a fixed-size array, where the array size doesn’t change during run time, the translation is a simple matter of allocating an equivalent array in C. Because the array size is known at compile time, you can allocate the array statically or on the stack, and there’s no need to resort to dynamic allocation. For example, the MATLAB function in Listing 5 maps one integer to another via a simple look-up table (LUT), which is a vector of fixed size. The equivalent C code, with LUT allocated on the stack, is shown in Listing 6. Similarly, you can allocate LUT in static memory, as shown in Listing 7. 34 Listing 4 Fixed- and variable-size arrays in MATLAB. % % F is a fixed-size array F=[1 2; % F is 2x2 0 0]; F(2,:)=[3 4]; % overwrite the second row % % V is a variable-size array V=[1 2]; % V is 1x2 V(2,:)=[3 4]; % append another row to V Listing 5 Look-up table (MATLAB). function y=lut_example(ind) % % map one integer to another % look up table N=1025; LUT=(0:N-1)+N; ind=mod(abs(ind),N); y=LUT(ind+1); Listing 6 Look-up table (C, stack allocation). // // map one integer to another int lut_stack(int ind) { int N=1025; int n; int lut[N]; // stack allocation // // look up table for (n=0;n<N;n++) { lut[n]=n+N; } ind=ind % N; return (lut[ind]); } A tool we developed for MATLABto-C translation takes the same approach to fixed-size arrays. When the size of a MATLAB array is known ahead of time, it simply allocates the equivalent array in C. By default, the translation tool uses the size of the array to determine whether to allocate the array on the stack, statically, or dynamically. However, you have complete control over this behavior, and, for ex- ample, can direct the translator to avoid all dynamic memory allocation. EMBEDDED C FOR VARIABLE-SIZE ARRAYS If your MATLAB code uses variablesize arrays, how do you translate them to C? The natural solution is to allocate an equivalent array dynamically, but in an environment that prohibits dynamic memory allocation, you have JUNE 2008 | embedded systems design | www.embedded.com http://www.embedded.com
Table of Contents Feed for the Digital Edition of Embedded Systems Design - June 2008 Embedded Systems Design - June 2008 Contents #Include Party Bit Programmer's Toolbox Cover Feature: Virtual Hardware Platforms for Embedded Software Validation Allocating Memory in MATLAB-to-C Code MDD and IDEs: Making the Twain Meet in Embedded Systems Design Avoid a Thrashing Guest Editor Advertising Index Break Points Marketplace Embedded Systems Design - June 2008 Embedded Systems Design - June 2008 - Embedded Systems Design - June 2008 (Page Cover1) Embedded Systems Design - June 2008 - Embedded Systems Design - June 2008 (Page Cover2) Embedded Systems Design - June 2008 - Embedded Systems Design - June 2008 (Page 1) Embedded Systems Design - June 2008 - Embedded Systems Design - June 2008 (Page 2) Embedded Systems Design - June 2008 - Contents (Page 3) Embedded Systems Design - June 2008 - Contents (Page 4) Embedded Systems Design - June 2008 - Contents (Page 5) Embedded Systems Design - June 2008 - Contents (Page 6) Embedded Systems Design - June 2008 - #Include (Page 7) Embedded Systems Design - June 2008 - #Include (Page 8) Embedded Systems Design - June 2008 - #Include (Page 9) Embedded Systems Design - June 2008 - Party Bit (Page 10) Embedded Systems Design - June 2008 - Party Bit (Page 11) Embedded Systems Design - June 2008 - Party Bit (Page 12) Embedded Systems Design - June 2008 - Party Bit (Page 13) Embedded Systems Design - June 2008 - Programmer's Toolbox (Page 14) Embedded Systems Design - June 2008 - Programmer's Toolbox (Page 15) Embedded Systems Design - June 2008 - Programmer's Toolbox (Page 16) Embedded Systems Design - June 2008 - Programmer's Toolbox (Page 17) Embedded Systems Design - June 2008 - Programmer's Toolbox (Page 18) Embedded Systems Design - June 2008 - Programmer's Toolbox (Page 19) Embedded Systems Design - June 2008 - Programmer's Toolbox (Page 20) Embedded Systems Design - June 2008 - Programmer's Toolbox (Page 21) Embedded Systems Design - June 2008 - Cover Feature: Virtual Hardware Platforms for Embedded Software Validation (Page 22) Embedded Systems Design - June 2008 - Cover Feature: Virtual Hardware Platforms for Embedded Software Validation (Page 23) Embedded Systems Design - June 2008 - Cover Feature: Virtual Hardware Platforms for Embedded Software Validation (Page 24) Embedded Systems Design - June 2008 - Cover Feature: Virtual Hardware Platforms for Embedded Software Validation (Page 25) Embedded Systems Design - June 2008 - Cover Feature: Virtual Hardware Platforms for Embedded Software Validation (Page 26) Embedded Systems Design - June 2008 - Cover Feature: Virtual Hardware Platforms for Embedded Software Validation (Page 27) Embedded Systems Design - June 2008 - Cover Feature: Virtual Hardware Platforms for Embedded Software Validation (Page 28) Embedded Systems Design - June 2008 - Cover Feature: Virtual Hardware Platforms for Embedded Software Validation (Page 29) Embedded Systems Design - June 2008 - Allocating Memory in MATLAB-to-C Code (Page 30) Embedded Systems Design - June 2008 - Allocating Memory in MATLAB-to-C Code (Page 31) Embedded Systems Design - June 2008 - Allocating Memory in MATLAB-to-C Code (Page 32) Embedded Systems Design - June 2008 - Allocating Memory in MATLAB-to-C Code (Page 33) Embedded Systems Design - June 2008 - Allocating Memory in MATLAB-to-C Code (Page 34) Embedded Systems Design - June 2008 - Allocating Memory in MATLAB-to-C Code (Page 35) Embedded Systems Design - June 2008 - Allocating Memory in MATLAB-to-C Code (Page 36) Embedded Systems Design - June 2008 - MDD and IDEs: Making the Twain Meet in Embedded Systems Design (Page 37) Embedded Systems Design - June 2008 - MDD and IDEs: Making the Twain Meet in Embedded Systems Design (Page 38) Embedded Systems Design - June 2008 - MDD and IDEs: Making the Twain Meet in Embedded Systems Design (Page 39) Embedded Systems Design - June 2008 - MDD and IDEs: Making the Twain Meet in Embedded Systems Design (Page 40) Embedded Systems Design - June 2008 - MDD and IDEs: Making the Twain Meet in Embedded Systems Design (Page 41) Embedded Systems Design - June 2008 - MDD and IDEs: Making the Twain Meet in Embedded Systems Design (Page 42) Embedded Systems Design - June 2008 - MDD and IDEs: Making the Twain Meet in Embedded Systems Design (Page 43) Embedded Systems Design - June 2008 - MDD and IDEs: Making the Twain Meet in Embedded Systems Design (Page 44) Embedded Systems Design - June 2008 - Avoid a Thrashing (Page 45) Embedded Systems Design - June 2008 - Avoid a Thrashing (Page 46) Embedded Systems Design - June 2008 - Avoid a Thrashing (Page 47) Embedded Systems Design - June 2008 - Guest Editor (Page 48) Embedded Systems Design - June 2008 - Guest Editor (Page 49) Embedded Systems Design - June 2008 - Guest Editor (Page 50) Embedded Systems Design - June 2008 - Guest Editor (Page 51) Embedded Systems Design - June 2008 - Advertising Index (Page 52) Embedded Systems Design - June 2008 - Break Points (Page 53) Embedded Systems Design - June 2008 - Break Points (Page 54) Embedded Systems Design - June 2008 - Marketplace (Page 55) Embedded Systems Design - June 2008 - Marketplace (Page 56) Embedded Systems Design - June 2008 - Marketplace (Page Cover3) Embedded Systems Design - June 2008 - Marketplace (Page Cover4)
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.