![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
This program explores how the compiler treats const string arrays.
main.cpp@0:07e4abe04bad, 2011-04-05 (annotated)
- Committer:
- hexley
- Date:
- Tue Apr 05 20:37:25 2011 +0000
- Revision:
- 0:07e4abe04bad
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hexley | 0:07e4abe04bad | 1 | /********************************************************************************* |
hexley | 0:07e4abe04bad | 2 | * This test program loads a large array of const strings, then reports how much |
hexley | 0:07e4abe04bad | 3 | * RAM is still available. |
hexley | 0:07e4abe04bad | 4 | * |
hexley | 0:07e4abe04bad | 5 | * Without the array, 24,196 bytes of RAM are free. |
hexley | 0:07e4abe04bad | 6 | * With an 8000-char array, 13,476 bytes of RAM are free |
hexley | 0:07e4abe04bad | 7 | * The const string of 8000 chars thus consumes 10720 bytes of RAM. |
hexley | 0:07e4abe04bad | 8 | *********************************************************************************/ |
hexley | 0:07e4abe04bad | 9 | #include "mbed.h" |
hexley | 0:07e4abe04bad | 10 | #include <string> |
hexley | 0:07e4abe04bad | 11 | |
hexley | 0:07e4abe04bad | 12 | DigitalOut fan(p21); // Needed for my breadboard |
hexley | 0:07e4abe04bad | 13 | |
hexley | 0:07e4abe04bad | 14 | #define TEST8000 // Comment this line to find the baseline value for free RAM |
hexley | 0:07e4abe04bad | 15 | |
hexley | 0:07e4abe04bad | 16 | #ifdef TEST8000 |
hexley | 0:07e4abe04bad | 17 | // teststring is an array of 100 x 80 character strings |
hexley | 0:07e4abe04bad | 18 | const string teststring[] = { |
hexley | 0:07e4abe04bad | 19 | "This is the first line of 100 80-character strings in the 'teststring[]' array.", |
hexley | 0:07e4abe04bad | 20 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 21 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 22 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 23 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 24 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 25 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 26 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 27 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 28 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 29 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 30 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 31 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 32 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 33 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 34 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 35 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 36 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 37 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 38 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 39 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 40 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 41 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 42 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 43 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 44 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 45 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 46 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 47 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 48 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 49 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 50 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 51 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 52 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 53 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 54 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 55 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 56 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 57 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 58 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 59 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 60 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 61 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 62 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 63 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 64 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 65 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 66 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 67 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 68 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 69 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 70 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 71 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 72 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 73 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 74 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 75 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 76 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 77 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 78 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 79 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 80 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 81 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 82 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 83 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 84 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 85 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 86 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 87 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 88 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 89 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 90 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 91 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 92 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 93 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 94 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 95 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 96 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 97 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 98 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 99 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 100 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 101 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 102 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 103 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 104 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 105 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 106 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 107 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 108 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 109 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 110 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 111 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 112 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 113 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 114 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 115 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 116 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 117 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 118 | "0123456789012345678901234567890123456789012345678901234567890123456789012345678", |
hexley | 0:07e4abe04bad | 119 | }; |
hexley | 0:07e4abe04bad | 120 | #endif |
hexley | 0:07e4abe04bad | 121 | |
hexley | 0:07e4abe04bad | 122 | int main() { |
hexley | 0:07e4abe04bad | 123 | int low = 0; |
hexley | 0:07e4abe04bad | 124 | int high = 0x8001; |
hexley | 0:07e4abe04bad | 125 | fan = 0; |
hexley | 0:07e4abe04bad | 126 | |
hexley | 0:07e4abe04bad | 127 | #ifdef TEST8000 |
hexley | 0:07e4abe04bad | 128 | // Include at least one reference to the array, to keep the optimizer happy |
hexley | 0:07e4abe04bad | 129 | printf("The first string is %s\r\n", teststring[0]); |
hexley | 0:07e4abe04bad | 130 | #endif |
hexley | 0:07e4abe04bad | 131 | |
hexley | 0:07e4abe04bad | 132 | // Compute free memory, using Segundo Equipo's AvailableMemory.cpp code |
hexley | 0:07e4abe04bad | 133 | __disable_irq(); |
hexley | 0:07e4abe04bad | 134 | while (high - low > 1) { |
hexley | 0:07e4abe04bad | 135 | int mid = (low + high) / 2; |
hexley | 0:07e4abe04bad | 136 | void* p = malloc(mid); |
hexley | 0:07e4abe04bad | 137 | if (p == NULL) { |
hexley | 0:07e4abe04bad | 138 | high = mid; |
hexley | 0:07e4abe04bad | 139 | } else { |
hexley | 0:07e4abe04bad | 140 | free(p); |
hexley | 0:07e4abe04bad | 141 | low = mid; |
hexley | 0:07e4abe04bad | 142 | } |
hexley | 0:07e4abe04bad | 143 | } |
hexley | 0:07e4abe04bad | 144 | __enable_irq(); |
hexley | 0:07e4abe04bad | 145 | |
hexley | 0:07e4abe04bad | 146 | printf("Test program 'stringtest' reports space available in RAM = %d\r\n", low); |
hexley | 0:07e4abe04bad | 147 | } |