Demonstrate what can happen when array limits are exceeded.

Dependencies:   mbed

Fork of SimpleConsoleTest by Charles Tritt

main.cpp

Committer:
CSTritt
Date:
2017-10-12
Revision:
2:80464803bc2f
Parent:
1:a000e0121191

File content as of revision 2:80464803bc2f:


/*
    File: main.cpp
    Project: ArrayLimits

    Demonstrates abuse of array limits on Nucleo boards.
    
    If necessary, avoid this with code like:
    
    if (index > SIZE - 1) {
        index = SIZE - 1;
    } else if (index < 0) {
        index = 0;
    }
    Use index...
    
    Created by Dr. C. S. Tritt; Last revised 10/12/17 (v. 1.0)
*/
#include "mbed.h"

int main()
{
    float a[] {1.0, 3.0, 5.0, 7.0, 9.0 };
    printf("Hello World !\n\n");

    for (int i = 0; i > -10; i--) { // Displays zeros and one NaN.
        printf("a[%d] is %f.\n", i, a[i]);
        wait(0.5f);
    }
    
    for (int i = 0; i < 10; i++) { // Hangs after 6th value!
        printf("a[%d] is %f.\n", i, a[i]);
        wait(0.5f);
    } 
    
    while (true) {}; // Just loop here forever after tests.
}