Re: Naive pointer question ? [message #28995 is a reply to message #28944] |
Thu, 24 January 2002 14:17   |
Bhautik Joshi
Messages: 21 Registered: November 2001
|
Junior Member |
|
|
>> <BIG snip>
>> p.s. Common blocks are not evil. They are unfairly maligned (and
> misaligned) but should be used
>> judiciously.... just like GOTOs.
>
> Sorry, I couldn't resist this!!! The problem with common blocks two-fold,
> both relating to their global scope (ie: the reason for using them in the
> first place). First, if you have two programs running in an IDL session
> (e.g. a pair of widget programs) which both use the same common block, each
> will corrupt the others data. In fact, you might even just be using two
> routines that don't realise another is accessing the common block. Secondly,
> you cannot trace the changes in a common block, so debugging becomes a
> nightmare. This is even worse in IDL when variables can change TYPE as well
> as value.
> That's it. Rant over:-) Let's just say, I gave them up a LONG time ago!-)
I'll concur with you there :)
Yes, when a lot of time is spent designing the program, common blocks
can be a reasonable and an easy alternative to more complex options.
However, things like goto and global variables went out of fashion a
very long time ago and have long since proven to be, in event driven
programming at least, unsafe and unpredictable. When you are maintaining
a piece of legacy code with common blocks all over the place you don't
really have any choice but to use them (or alternatively rebuild the
whole damn thing from the ground up!) but, I for one prefer to avoid
them.
In terms of event driven programming, without proper logical locking
mechanisms (mutual exclusion -> semaphores/monitors etc.), global
variables are dangerous. Your widget program that reads and writes to,
or worse, depends on a global variable shared between many widgets all
doing things at different times, might get into a race condition and
then things get *really* ugly. I can't really think of an example off
the top of my head, so go see:
http://www.cs.mtu.edu/~shene/NSF-3/e-Book/RACE/1st-attempt.h tml
(just something I found via google)
Perhaps I'm just nuts and its too early in the morning for me; please
correct me if I'm wrong :)
cheers,
Bhautik
--
/--------------------------------------------------(__)----- ----\
| nbj@imag.wsahs.nsw.gov.au | phone: 0404032617 |..|--\ -moo |
| ICQ #: 2464537 | http://cow.mooh.org | |--| |
|---------------------------+----------------------\OO/|| ------/
| international |
| roast. my sanity has gone |
| its lost forever |
\---------------------------/
|
|
|