Demonstrate what can happen when array limits are exceeded.
Fork of SimpleConsoleTest by
main.cpp@2:80464803bc2f, 2017-10-12 (annotated)
- Committer:
- CSTritt
- Date:
- Thu Oct 12 20:35:08 2017 +0000
- Revision:
- 2:80464803bc2f
- Parent:
- 1:a000e0121191
Cleaned up comments and output spacing.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
CSTritt | 1:a000e0121191 | 1 | |
CSTritt | 1:a000e0121191 | 2 | /* |
CSTritt | 1:a000e0121191 | 3 | File: main.cpp |
CSTritt | 1:a000e0121191 | 4 | Project: ArrayLimits |
CSTritt | 0:2f9e67d4c561 | 5 | |
CSTritt | 2:80464803bc2f | 6 | Demonstrates abuse of array limits on Nucleo boards. |
CSTritt | 2:80464803bc2f | 7 | |
CSTritt | 2:80464803bc2f | 8 | If necessary, avoid this with code like: |
CSTritt | 2:80464803bc2f | 9 | |
CSTritt | 2:80464803bc2f | 10 | if (index > SIZE - 1) { |
CSTritt | 2:80464803bc2f | 11 | index = SIZE - 1; |
CSTritt | 2:80464803bc2f | 12 | } else if (index < 0) { |
CSTritt | 2:80464803bc2f | 13 | index = 0; |
CSTritt | 2:80464803bc2f | 14 | } |
CSTritt | 2:80464803bc2f | 15 | Use index... |
CSTritt | 2:80464803bc2f | 16 | |
CSTritt | 2:80464803bc2f | 17 | Created by Dr. C. S. Tritt; Last revised 10/12/17 (v. 1.0) |
CSTritt | 1:a000e0121191 | 18 | */ |
CSTritt | 1:a000e0121191 | 19 | #include "mbed.h" |
CSTritt | 0:2f9e67d4c561 | 20 | |
CSTritt | 0:2f9e67d4c561 | 21 | int main() |
CSTritt | 0:2f9e67d4c561 | 22 | { |
CSTritt | 1:a000e0121191 | 23 | float a[] {1.0, 3.0, 5.0, 7.0, 9.0 }; |
CSTritt | 2:80464803bc2f | 24 | printf("Hello World !\n\n"); |
CSTritt | 1:a000e0121191 | 25 | |
CSTritt | 1:a000e0121191 | 26 | for (int i = 0; i > -10; i--) { // Displays zeros and one NaN. |
CSTritt | 1:a000e0121191 | 27 | printf("a[%d] is %f.\n", i, a[i]); |
CSTritt | 1:a000e0121191 | 28 | wait(0.5f); |
CSTritt | 0:2f9e67d4c561 | 29 | } |
CSTritt | 1:a000e0121191 | 30 | |
CSTritt | 1:a000e0121191 | 31 | for (int i = 0; i < 10; i++) { // Hangs after 6th value! |
CSTritt | 1:a000e0121191 | 32 | printf("a[%d] is %f.\n", i, a[i]); |
CSTritt | 1:a000e0121191 | 33 | wait(0.5f); |
CSTritt | 1:a000e0121191 | 34 | } |
CSTritt | 1:a000e0121191 | 35 | |
CSTritt | 1:a000e0121191 | 36 | while (true) {}; // Just loop here forever after tests. |
CSTritt | 0:2f9e67d4c561 | 37 | } |