I have an executable that crashes after main has finished all its instructions, and the last assembler instruction is executed. Just before the program crashes, I do disas on GDB:

   0x2001c054 <+84>:    movt    r3, #16384      ; 0x4000
   0x2001c058 <+88>:    ldrh    r3, [r3, #16]
   0x2001c05a <+90>:    uxth    r3, r3
   0x2001c05c <+92>:    str     r3, [sp, #0]
   0x2001c05e <+94>:    bl      0x2001c0f8 <scream>
   0x2001c062 <+98>:    mov.w   r3, #0
   0x2001c066 <+102>:   mov     r0, r3
   0x2001c068 <+104>:   add     sp, #28
=> 0x2001c06a <+106>:   pop     {pc}
End of assembler dump.

Now if I go to the next instruction (ni in GDB) I get the following in GDB:

0xfffffffe in ?? ()

Also, OpenOCD (which I use to communicate with my STM32F2 board) gives the following error:

Error: address + size wrapped(0xfffffffe, 0x00000004)

How can I tell my executable to exit gracefully?

  • 1
    \$\begingroup\$ You could use an infinite loop, or just set the controller in a wait state, better if standby or sleep mode \$\endgroup\$ – clabacchio Mar 22 '12 at 16:37
  • \$\begingroup\$ Yes, but that feels like a hack. \$\endgroup\$ – Randomblue Mar 22 '12 at 17:04
  • 4
    \$\begingroup\$ It's not a "hack". Do you think that Linux or Windows' kernels ever return from main? A program on a microcontroller (unless running under an existing OS) is more like an OS kernel. Never exit. \$\endgroup\$ – Toby Jaffey Mar 22 '12 at 17:21
  • \$\begingroup\$ Yeah, Joby is right - that's how microcontrollers work. Expand your mind man. It's a whole new world down here. \$\endgroup\$ – AngryEE Mar 23 '12 at 15:13

Is this on a microcontroller? If so, you should never exit main() - use an infinite loop instead.


If this is an operating system or other app on the bare metal, main should never exit. If you really must exit, then your C startup code could reset the board after main.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.