6.1.1260 DROP CORE

( x -- )

Remove x from the stack.


T{ 1 2 DROP -> 1 }T
T{ 0   DROP ->   }T


kumaymavatar of kumaym [323] stack checking Suggested reference implementation2023-12-22 22:14:01

Is there any standard about checking for stack underflow when using the word DROP (and similar ones)? I'd also like to know if there's overflow checking in words like DUP and similar

AntonErtlavatar of AntonErtl

The standard mentions stack overflow and underflow as ambiguous conditions in section 4.1.2. Therefore, standard programs must not overflow or underflow these stacks (and I expect one can find related wording for other stacks). Standard systems can do anything if they do.

Many Forth systems do not check for stack overflow or underflow, or check only the stack depth in the text interpreter (i.e., there may have been a temporary underflow in the meantime, which is not reported); the over- or underflow can overwrite other data on these systems.

One exception is the gforth debugging engine on an OS with mmap() (e.g., Linux); there the stacks are bounded by unaccessible pages, and every stack underflow or overflow leads to an appropriate exception. Note that the engine gforth-fast does not report most underflows, but the stacks are still surrounded by unaccessible pages, which prevents data corruption on underflow.

Reply New Version