[COFF] [TUHS] C vs Pascal thoughts - was Buffer overflow found/fixed in v4 tape ; )
Bakul Shah via COFF
coff at tuhs.org
Wed Jan 7 03:41:47 AEST 2026
On Jan 6, 2026, at 8:37 AM, Paul Winalski <paul.winalski at gmail.com> wrote:
>
> On Mon, Jan 5, 2026 at 5:31 PM Bakul Shah via COFF <coff at tuhs.org> wrote:
>
> 2.2 static variables -- I tend to believe using static vars is usually
> a mistake. Initialization -- saves some typing but that is about it.
>
> Static variables are useful where a subroutine must retain context betwen invocations. For example, the seed for a pseudo-random number generator.
>
> -Paul W.
Static variables are essentially the same as global variables except with local scope. This is a problem for concurrency. A better solution is to use nested procedures such as (using Scheme syntax)
(define (make-random seed) ; return a function returning a random number
(lambda ()
(let ((return-value (some-randomizing-function seed)))
(set! seed (new-seed seed))
return-value))))
(define my-random (make-random 314159265))
(my-random)
(my-random) etc. Now you can give each thread its own random function.
This is also why we have "reentrant" versions of common functions. For example readdir_r(3) that will work predictably if two separate threads try to read dir entries from the same opened dir.
Also note that newer languages "frown" upon global variables. In V for example you have to pass a command line arg to allow any global variables! Global constants are fine. Personally I would define a "shared" attribute and allow shared globals -- the idea being any access to shared variables is protected via mutexes -- Idea I first saw in Brinch Hansen's red OS book (operating system principles). Here's the orig paper: https://dl.acm.org/doi/epdf/10.1145/361454.361473
More information about the COFF
mailing list