www.delorie.com/djgpp/doc/kb/kb_18.html   search  
DJGPP Knowledge Base

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.3.6 Coping with restarted programs

Uninitialised static and global data is placed in the .bss section. BSS stands for Block Started by Symbol. The .bss section is zeroed by the start-up code.

Initialised static and global data is placed in the .data section.

So it would seem that one can rely on static and global variables being initialised to zero or some specified value. Unfortunately this may not be true, where programs are unexecuted and then restarted --- Emacs is the primary example.

Part of Emacs's build procedure is that Emacs unexecutes (dumps) itself to a file. The dumping process records the value of each variable at the time of the dump to a file. The symbols in the .bss section are moved to the .data section. The values at the time of the dump are recorded, not the original values. So if the library code is relying on the value of a static or global variable, e.g., to see whether it needs to initialise some device or allocate memory, then it may break when the program restarts.

Fortunately there is a way that library code can detect a restart. The variable __bss_count contains a counter of the number of times that the program has been started -- it is incremented by the start-up code. A routine can store __bss_count and then check whether it needs to initialise its data by comparing __bss_count with its stored value. Here is an example:

#include <string.h>
#include <libc/bss.h>

extern int do_something_interesting (const int n);

static int my_bss_count = -1;
static int my_array[20];

myfunc (const int n)
  if (my_bss_count != __bss_count)
      my_bss_count = __bss_count;
      memset(my_array, 0, sizeof(my_array));

  if (n >= 20)
    return 0;

  if (!my_array[n])
    my_array[n] = do_something_interesting(n);


For more details see `src/libc/crt0/crt1.c' in the DJGPP libc sources.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2004   by DJ Delorie     Updated Nov 2004