Article View: comp.lang.c.moderated
Article #2229Re: Some questions on coding
From: Blair P Houghton
Date: Sun, 10 Mar 1996 16:32
Date: Sun, 10 Mar 1996 16:32
93 lines
3161 bytes
3161 bytes
In article <4hqef9$1oj@solutions.solon.com>,
Herman Rubin <hrubin@stat.purdue.edu> wrote:
>Question 1. Is there a quick way of using a variable label?
Labels are identifiers, but not variable objects. You will
have to use if's or switches and hardcoded gotos.
>Transfers, loads, and stores take a
>significant part of the running time of the algorithm, and
>so something like setjmp and longjmp are to be avoided.
Setjmp and longjmp don't do what you want; they would more
correctly be called "checkpoint" and "restart," since they
only guarantee that calling longjmp gets you the same
program state as the corresponding call to setjmp.
(They're so-so for the recovery-block style of
fault-tolerant computing, but aren't acceptable as
long-distance gotos).
>Question 2. Is there a way of declaring "local", so that
>labels in one block of a coding unit can be the same as
>labels in another? The labels are within the block only,
>but the various blocks are connected with gotos, not calls.
Gotos are limited to the function in which they're found.
The virtual machine C defines is a stack-based computer;
it's not kosher to jump up and down a branch of the stack
at will. (Even when longjmp does it, it does so by unwinding
the stack, destructing all of the newer stack frames. Kind
of a bungee-jump, if you will...)
To do this, though, you can organize your subroutines as
statement blocks in a single function. It means you have to
mimic argument-passing and the stack's auto-variable creation
yourself. Presumably these routines are already reentrant,
or you wouldn't be thinking of entering them from the side.
Give each block a beginning label, and make sure each one
ends with a return statement (as a safety buffer), and
you're implementing functions the way the compiler lays
them down in the assembly code (gcc often leaves two
ret instructions at the end of a routine; one because of
its template-based idea of translation, and one because
the source contained one explicitly):
int main( void )
{
goto bar;
foo: {
...
goto bar;
...
}
return;
bar: {
...
goto foo;
...
}
return;
}
This is behaviorally equivalent to:
int main( void )
{
bar();
}
void foo( void )
{
...
bar();
...
}
void bar( void )
{
...
foo();
...
}
Some modern microprocessors have branch-and-link
instructions that provide light-weight function calls; gcc
uses these extensively with the i960. You might want to
try splitting your current routines into smaller ones that
call each other a lot.
--Blair
"10 Read : Post : Goto 10"
Technical Details
Message-ID:
<4hv078$jcj@solutions.solon.com>Path:
arc1-ca.pugleaf.net!solon.com!not-for-mailReferences:
<4hqef9$1oj@solutions.solon.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail From: blair@trojan.neta.com (Blair P Houghton) Newsgroups: comp.lang.c.moderated Subject: Re: Some questions on coding Date: 10 Mar 1996 10:32:40 -0600 Organization: Internet Access, Chandler AZ. Sender: clc@solutions.solon.com Approved: clc@solutions.solon.com Message-ID: <4hv078$jcj@solutions.solon.com> References: <4hqef9$1oj@solutions.solon.com> NNTP-Posting-Host: solutions.solon.com