Printing out array using for loop messes up the entire program

Hi, I'm using the Itsybisy NRF52840 Express board and building the program with west.

I'm trying to print out my array using for loop. Weirdly, if I print out the elements in for loop, the entire program stops and the red flash button (next to the rest button) blinks twice very fast and stops and repeats.

If I print out the same elements without a for loop, things work as predicted. Here is the code.

// Version 1: for loop stops the program
uint8_t image[19200] = {0,2,3, ...};

printk("Image: );
for (int i = 0; i < 3; i++) {
    printk("%u ", image[i]);
}
printk("\n");

// Version 2: predictive behavior
uint8_t image[19200] = {0,2,3, ...};

printk("Image: );
printk("%u ", image[0]);
printk("%u ", image[1]);
printk("%u ", image[2]);
printk("\n");

What could possibly be wrong? Any lead would be appreciated!

Parents
  • Hi 

    Jörg is on to it. So called automatic variables (neither static nor global) will be put on the stack, and since the stack is usually only a couple of kilobytes large you will run into a stack overflow if you generate large automatic variables. 

    The quick fix is simply to make the variable static or global, then it will not be put on the stack, and you should avoid the stack overflow. 

    You could also make the stack much larger, but in general I wouldn't recommend putting variables this large on the stack. 

    Best regards
    Torbjørn

Reply
  • Hi 

    Jörg is on to it. So called automatic variables (neither static nor global) will be put on the stack, and since the stack is usually only a couple of kilobytes large you will run into a stack overflow if you generate large automatic variables. 

    The quick fix is simply to make the variable static or global, then it will not be put on the stack, and you should avoid the stack overflow. 

    You could also make the stack much larger, but in general I wouldn't recommend putting variables this large on the stack. 

    Best regards
    Torbjørn

Children
No Data
Related