C doubts

S

sophia.agnes

1)can any one give an example for a C program that can be written
using goto only ? i.e a program in which we cannot avoid the keyword
goto

2)what exactly is the purpose of tmpfile() ? will it open a temporary
file ?

3)why there is no article on dangling pointer in the FAQ ?
 
J

jameskuyper

1)can any one give an example for a C program that can be written
using goto only ? i.e a program in which we cannot avoid the keyword
goto

No. The 'goto' keyword is only a convenience. There are circumstances
where it simplifies a routine considerably, but it's never a necessity
2)what exactly is the purpose of tmpfile() ? will it open a temporary
file ?

Sometimes you want to create a file simply to store data that will
only be read back during the same run of the same program. tmpfile()
opens such a file, without you having to bother to choose a name for
it. Keep in mind that different operating systems have different file-
naming requirements; a name that works on your system might fail on
another. For portability, you could use tmpnam() to select the file
name, followed by fopen(), but in principle it's possible that some
other process will open a file with that name between the call to
tmpnam() and fopen(). tmpfile() is a much simpler solution.
3)why there is no article on dangling pointer in the FAQ ?

I don't know the answer to that one.
 
S

santosh

1)can any one give an example for a C program that can be written
using goto only ? i.e a program in which we cannot avoid the keyword
goto

If there is such a program it's likely to be contrived. Most real world
programs can avoid goto if necessary, though most make *some* use of
it.
2)what exactly is the purpose of tmpfile() ? will it open a temporary
file ?

Yes. It returns a pointer to a stream which will be valid until it is
closed via fclose() or by normal program termination.
3)why there is no article on dangling pointer in the FAQ ?

See question 7.1
 
J

jacob navia

1)can any one give an example for a C program that can be written
using goto only ? i.e a program in which we cannot avoid the keyword
goto

This code snippet demonstrates that:

switch (integer) {
case 534:
switch (n) {
case 254:
goto goon;
case 566:
//
break;
}
printf("Sub-item for 534 not found");
break;
}
return 0;
goon:
return 6445;
}

Obviously that *could* be rearranged but the point is that the language
does not provide a "named" break statement. If you want to break from
nested switches, nested loops, or nested constructs like that you have
to use the goto statement
2)what exactly is the purpose of tmpfile() ? will it open a temporary
file ?

Read the documentation
Yes, it will create a file
3)why there is no article on dangling pointer in the FAQ ?
Really? It should be there somehow, can't imagine that they missed that.
 
W

Walter Roberson

1)can any one give an example for a C program that can be written
using goto only ? i.e a program in which we cannot avoid the keyword
goto

I'm not sure, but possibly the following:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void) {
srand( (unsigned int) time(0) );
if (rand() < 1234)
goto FOO;
{ const unsigned char bar = 17;
FOO: printf("%d\n", (int) bar);
}
return 0;
}

This -relies- upon bar having an indeterminate value at the time
of the printf. The value will not be a trapping value because
unsigned char never has trapping values. The usual method of
rewriting this to avoid the goto would be to have something like

do_inner_initialize = 1;
if (rand() < 1234) {
do_inner_initialize = 0;
}
{
const unsigned char bar;
if (do_inner_initialize) {
bar = 17;
}
printf("%d\n", (int) bar);
}

However, with bar being const, I'm not sure that it can be initialized
after the declaration. (The C89 document says little about initializing
const objects.)
 
C

CBFalconer

1) can any one give an example for a C program that can be written
using goto only ? i.e a program in which we cannot avoid the
keyword goto

It has been shown (long ago) that any goto implemented program can
be implemented with only if and while.
2) what exactly is the purpose of tmpfile() ? will it open a
temporary file ?

3) why there is no article on dangling pointer in the FAQ ?

I have no idea what you are talking about here. Please reference
the place in the C standard that defines these things.
 
S

sophia.agnes

If there is such a program it's likely to be contrived. Most real world
programs can avoid goto if necessary, though most make *some* use of
it.


Yes. It returns a pointer to a stream which will be valid until it is
closed via fclose() or by normal program termination.


See question 7.1

Question 7.1 points to this link:-
http://c-faq.com/malloc/malloc1.html
is n't it?

but it does n't say any thing about dangling pointer
 
R

Richard

santosh said:
If there is such a program it's likely to be contrived. Most real world
programs can avoid goto if necessary, though most make *some* use of
it.

In my reasonably extensive experience of large C projects goto is not
used in most at all. A tiny minority and then only in parsers.

Personally I dont have anything at all against a well documented goto()
- it's a snobbery thing for most people.

If(condition) jump to point. Nothing could be easier to understand when
not over used and made in a nasty ill formed spaghetti dish.
 
C

CBFalconer

jacob said:
This code snippet demonstrates that:

switch (integer) {
case 534:
switch (n) {
case 254:
goto goon;
case 566:
//
break;
}
printf("Sub-item for 534 not found");
break;
}
return 0;
goon:
return 6445;
}

Obviously that *could* be rearranged but the point is that the
language does not provide a "named" break statement. If you want
to break from nested switches, nested loops, or nested constructs
like that you have to use the goto statement

Nonsense. The computer world has known better for about 50 years.
IF and WHILE suffice for any language.
 
F

Flash Gordon

jacob navia wrote, On 15/11/07 19:07:
This code snippet demonstrates that:

switch (integer) {
case 534:
switch (n) {
case 254:
goto goon;
case 566:
//
break;
}
printf("Sub-item for 534 not found");
break;
}
return 0;
goon:
return 6445;
}

Obviously that *could* be rearranged

Very easily, so it is not an example that demonstrates your point.
but the point is that the language
does not provide a "named" break statement. If you want to break from
nested switches, nested loops, or nested constructs like that you have
to use the goto statement

Or a flag, or sometimes you can do it by using another layer of function
and a return, or...

I believe that C would still be Turing complete without goto, i.e. there
are no programs for which goto is required. However, that does not mean
it is advisable to avoid using goto just for the sake of avoiding using
goto.

<snip>
 
R

Richard Harter

1)can any one give an example for a C program that can be written
using goto only ? i.e a program in which we cannot avoid the keyword
goto

No, yes, no, and yes. The answer is no because every program
that contains gotos can be rewritten using only structured
constructs. The answer is yes because such rewrites are not
equivalent to the original program in terms of execution time and
program space. The answer is no because such programs cannot be
effectively written by human beings. The answer is yes because
such programs can be generated by other programs.
2)what exactly is the purpose of tmpfile() ? will it open a temporary
file ?

3)why there is no article on dangling pointer in the FAQ ?

Richard Harter, (e-mail address removed)
http://home.tiac.net/~cri, http://www.varinoma.com
In the fields of Hell where the grass grows high
Are the graves of dreams allowed to die
 
S

Szabolcs Nagy

1)can any one give an example for a C program that can be written
using goto only ? i.e a program in which we cannot avoid the keyword
goto

void compact()
{
if (f())
goto F;
if (g())
goto G;
doX();
return; /* this can be omitted, the goto is still needed */

F: doY();
G: doZ();
}

/* same without goto */
void bloat()
{
if (f()) {
doY();
doZ();
return;
}
if (g()) {
doZ();
return;
}
doX();
}

without goto you either repeat some code (doZ() in this example) which
can be painful if you have many local variables, many nested loops
etc, or double check one of the return values (f(), g())

this situation typically occures with resource allocations:

void resources() {
resource_t r1, r2;

r1 = alloc_res1();
if (!r1)
goto fail1;
r2 = alloc_res2();
if (!r2)
goto fail2;
use(r1, r2);
/* return 1; */ /* depends on the situation */

release_res2(r2);
fail2: release_res1(r1);
fail1:
/* return 0; */
}


another favourite example:

void loop()
{
while(f())
if (g())
goto G;
x();
G:
y();
}

same situation as above
goto can be avoided if while had an else clause for the case when the
loop condition fails

/* not c code */
void loop()
{
while(f()) {
if (g())
break;
} else { /* not c */
x();
}
y();
}

with any other solution you either repeat code or double check one of
the conditions.

(of course double checking a condition is not too much waste of time,
also in certain situations the compiler can be clever and do the
optimizations for you)
 
E

Eric Sosman

1)can any one give an example for a C program that can be written
using goto only ? i.e a program in which we cannot avoid the keyword
goto

It depends on what you mean by "a C program."

In a formal sense, it's easy to see that goto can always
be eliminated. (Somebody or other proved this in a paper
that always gets cited, but it always seemed to me that they
had proved the sky was blue. But then, I haven't read the
paper.)

However, the source code changes to eliminate goto might
make a particular program larger than it had been, and it
might become too big for the compiler to compile or for the
host computer to execute. So it is at least conceivable that
there exists at least one (very large) program which would
cease to function if the goto's were eliminated. Constructing
such a program would probably require a very large effort.
2)what exactly is the purpose of tmpfile() ? will it open a temporary
file ?

It creates and opens a temporary file. "Temporary"
means that the file will be deleted from the permanent file
storage when the program ends, even if the program doesn't
call remove() on it. It can be useful when a program must
process large amounts of data in multiple "passes," where
one pass writes to the temporary file and others read it
back in again.
3)why there is no article on dangling pointer in the FAQ ?

Maybe the questions about dangling pointers aren't FA.
 
E

Eric Sosman

Szabolcs Nagy wrote On 11/15/07 17:20,:
void compact()
{
if (f())
goto F;
if (g())
goto G;
doX();
return; /* this can be omitted, the goto is still needed */

F: doY();
G: doZ();
}

void compact(void) {
int quo = 17;
while (quo != 42) {
switch (quo) {
case 44:
doY();
quo = 666;
break;
case 43:
doX();
quo+=-1;
break;
case 17:
quo = f() ? 44 : 31;
break;
case 51;
doZ();
quo -= 9;
break;
case 31:
quo += g() ? 20 : 12;
break;
}
}
}

Structured control flow at its very best.
 
K

Kenneth Brody

Walter said:
I'm not sure, but possibly the following:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void) {
srand( (unsigned int) time(0) );
if (rand() < 1234)
goto FOO;
{ const unsigned char bar = 17;
FOO: printf("%d\n", (int) bar);
}
return 0;
}

This -relies- upon bar having an indeterminate value at the time
of the printf. The value will not be a trapping value because
unsigned char never has trapping values. The usual method of
rewriting this to avoid the goto would be to have something like
[...]

Would this code do the same thing?

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void) {
srand( (unsigned int) time(0) );
switch ( rand() < 1234 )
{
case 0:
{
const unsigned char bar = 17;
case 1:
printf("%d\n",bar);
}
}
return 0;
}

Those nasty inner braces can be gotten rid of if your compiler
supports variables declared within the code.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
D

Dik T. Winter

> In article
> <59873ca2-b9a9-4e2a-87a9-4d0cc4844b4f@e10g2000prf.googlegroups.com>,
> Nov 2007 11:52 pm:
>
>
> If there is such a program it's likely to be contrived. Most real world
> programs can avoid goto if necessary, though most make *some* use of
> it.

I do not know whether it applies, but back in the days of Algol 60 there
hase been a discussion between van Wijgaarden and somebody else about the
possibility to rewrite every program into a gotoless program. At that
time van Wijngaarden came up to transform a program with goto's into a
program without goto's. (Strange enough the process first eliminated
every function call into something containing a lot of goto's. And after
that it changed goto's into function calls...) It worked for Algol 60,
I would now know why it should not also work for C, there are a lot of
similarities, the only main major block might be the lack of nested
functions in C. But off-hand I would state that every program using
goto's can be rewritten into a goto-less program.
 
J

jameskuyper

Szabolcs said:
void compact()
{
if (f())
goto F;
if (g())
goto G;
doX();
return; /* this can be omitted, the goto is still needed */

F: doY();
G: doZ();
}

/* same without goto */
void bloat()
{
if (f()) {
doY();
doZ();
return;
}
if (g()) {
doZ();
return;
}
doX();
}

You yourself have just demonstrated that it doesn't need 'goto'. She
didn't ask for examples where 'goto' was convenient, she asked for
examples where use of 'goto' cannot be avoided.
 
A

Al Balmer

I do not know whether it applies, but back in the days of Algol 60 there
hase been a discussion between van Wijgaarden and somebody else about the
possibility to rewrite every program into a gotoless program.

Look up Boehm - Jacopini.

IBM had a published algorithm for converting arbitrary programs into
structured (goto-less) programs.
 
S

Szabolcs Nagy

You yourself have just demonstrated that it doesn't need 'goto'. She
didn't ask for examples where 'goto' was convenient, she asked for
examples where use of 'goto' cannot be avoided.

nope
unfortunately you haven't quoted the important part
the two code is _not_ the same

there is no compiler which can always determine they do the same so
they will be generate different programs with different
characteristics

so in these examples 'goto' _cannot_ be avoided if one wants to be
pedantic about the generated code as well (not just semantics)

actually for turing completness very few satetement would be enough: a
mere while would do! [1] (no functions, no if, no break, no goto.. is
needed)

eg.

if(c())
A();
else
B();

can be implemented with while as:

run = 1;
while(c() && run) {
A();
run = 0;
}
while(run) {
B();
run = 0;
}

so by your logic if..else, ?:, switch,.. in c is not necessary and if
anyone asks wether there are programs which absolutely need them then
we should answer no

also this logic implies bubblesort is the same as quicksort, they both
sort an array of elements


[1]: it is easy to see that you can rewrite a brainfuck program in c
with only 'while' and brainfuck is proven to be turing complet.
 
R

Richard Heathfield

Szabolcs Nagy said:

so in these examples 'goto' _cannot_ be avoided

This doesn't follow. All it shows is that you have not avoided goto in
these examples.

The goto keyword is always avoidable. In practical terms, the time to avoid
it is at the specification stage. When you're deciding what the program
must do, you don't think in terms of goto, but in terms of modules.
Properly carried out, functional decomposition will allow you to design a
gotoless solution if that's what you want. So *first* decide what you want
to do, and write it down not in C but in English. Then you should easily
be able to design a solution that doesn't use goto.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,815
Messages
2,569,702
Members
45,492
Latest member
juliuscaesar

Latest Threads

Top