Article View: comp.lang.c.moderated
Article #2227Re: Some questions on coding
From: Konrad Schwarz
Date: Sun, 10 Mar 1996 16:32
Date: Sun, 10 Mar 1996 16:32
65 lines
3134 bytes
3134 bytes
In article <4hqef9$1oj@solutions.solon.com>, hrubin@stat.purdue.edu (Herman Rubin) writes:
|> Question 1. Is there a quick way of using a variable label?
|> The way it would be used is to identify the label to which one
|> wants to transfer, and then have some other place or places
|> transfer to that point? 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.
An approach in ANSI C: use a giant switch statement inside of an endless loop
and program something similar to a finite-state machine. Map labels to
integers, use a integer variable as the controlling expression of the switch
statement, and when you want to use a goto, assign the number of the
appropriate label to the integer variable and jump to the switch statement.
For example:
register label = 0;
dispatch: switch (label) {
case 10:
do_something;
label = 12;
goto dispatch;
...
}
The disadvantage is that twice as many jumps are performed.
GCC has extensions that allow labels to be ASSIGNed, as in FORTRAN. See
the gcc info manual.
|> 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.
ANSI C does not define ``coding unit.'' Labels in ANSI C have function
scope, i.e., labels must be unique inside of a function, but duplicate
labels can turn up in different functions, even if the functions are
defined in the same file, (technically, translation unit). This is similar
to the FORTRAN rule.
If I assume ``coding unit'' to mean function, then no, there are no local labels,
but again, GCC might be extended in this respect.
A general solution is to use a macro preprocessor such as m4 to generate
label names. The C preprocessor might be powerful enough for your needs, e.g.,
if label names can be made unique through concatenation of predefined parts.
Example:
# define CODE_WITH_TWO_LABELS(CODE1,CODE2,CODE3,UNIQUE_IDENTIFIER)\
CODE1\
first_label_ ## UNIQUE_IDENTIFIER:\
CODE2\
second_label_ ## UNIQUE_IDENTIFIER:\
CODE3
CODE_WITH_TWO_LABELS (a = b + c; d = e + f;, g = h - i; j = k - l;, m = n * o;
p = q * r;, section_54)
The label names are ``first_label_section_54'' and ``second_label_section_54''.
Note that any use of the comma operator in CODE1, CODE2, and CODE3 must
be enclosed in parenthesis, otherwise the comma is incorrectly understood
as an argument separator when expanding an invocation of CODE_WITH_TWO_LABELS.
Konrad Schwarz
Technical Details
Message-ID:
<4hv065$jao@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: schwarz@a2.complang.tuwien.ac.at (Konrad Schwarz) Newsgroups: comp.lang.c.moderated Subject: Re: Some questions on coding Date: 10 Mar 1996 10:32:05 -0600 Organization: TU Wien Sender: clc@solutions.solon.com Approved: clc@solutions.solon.com Message-ID: <4hv065$jao@solutions.solon.com> References: <4hqef9$1oj@solutions.solon.com> NNTP-Posting-Host: solutions.solon.com