C doubts

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sophia.agnes@gmail.com

    #1

    C doubts

    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 ?
  • santosh

    #2
    Re: C doubts

    In article
    <59873ca2-b9a9-4e2a-87a9-4d0cc4844b4f@e1 0g2000prf.googl egroups.com>,
    sophia.agnes@gm ail.com <sophia.agnes@g mail.comwrote on Thursday 15
    Nov 2007 11:52 pm:
    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

    Comment

    • sophia.agnes@gmail.com

      #3
      Re: C doubts

      On Nov 15, 1:38 pm, santosh <santosh....@gm ail.comwrote:
      In article
      <59873ca2-b9a9-4e2a-87a9-4d0cc4844...@e1 0g2000prf.googl egroups.com>,
      sophia.ag...@gm ail.com <sophia.ag...@g mail.comwrote on Thursday 15
      Nov 2007 11:52 pm:
      >
      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
      Question 7.1 points to this link:-

      is n't it?

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

      Comment

      • Flash Gordon

        #4
        Re: C doubts

        jacob navia wrote, On 15/11/07 19:07:
        sophia.agnes@gm ail.com wrote:
        >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
        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>
        --
        Flash Gordon

        Comment

        • Richard Harter

          #5
          Re: C doubts

          On Thu, 15 Nov 2007 10:22:04 -0800 (PST), sophia.agnes@gm ail.com
          wrote:
          >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, cri@tiac.net
          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

          Comment

          • Szabolcs  Nagy

            #6
            Re: C doubts


            sophia.ag...@gm ail.com wrote:
            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)

            Comment

            • Eric Sosman

              #7
              Re: C doubts

              sophia.agnes@gm ail.com wrote On 11/15/07 13:22,:
              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.

              --
              Eric.Sosman@sun .com

              Comment

              • Eric Sosman

                #8
                Re: C doubts

                Szabolcs Nagy wrote On 11/15/07 17:20,:
                sophia.ag...@gm ail.com wrote:
                >
                >>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();
                }
                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.

                --
                Eric.Sosman@sun .com

                Comment

                • Kenneth Brody

                  #9
                  Re: C doubts

                  Walter Roberson wrote:
                  >
                  In article <59873ca2-b9a9-4e2a-87a9-4d0cc4844b4f@e1 0g2000prf.googl egroups.com>,
                  <sophia.agnes@g mail.comwrote:
                  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
                  [...]

                  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",b ar);
                  }
                  }
                  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:ThisIsA SpamTrap@gmail. com>

                  Comment

                  • Dik T. Winter

                    #10
                    Re: C doubts

                    In article <fhi3o5$cqg$1@r egistered.motza rella.orgsantos h <santosh.k83@gm ail.comwrites:
                    In article
                    <59873ca2-b9a9-4e2a-87a9-4d0cc4844b4f@e1 0g2000prf.googl egroups.com>,
                    sophia.agnes@gm ail.com <sophia.agnes@g mail.comwrote on Thursday 15
                    Nov 2007 11:52 pm:
                    >
                    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.
                    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.
                    --
                    dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
                    home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/

                    Comment

                    • jameskuyper@verizon.net

                      #11
                      Re: C doubts

                      Szabolcs Nagy wrote:
                      sophia.ag...@gm ail.com wrote:
                      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();
                      }
                      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.

                      Comment

                      • Al Balmer

                        #12
                        Re: C doubts

                        On Thu, 15 Nov 2007 23:58:39 GMT, "Dik T. Winter" <Dik.Winter@cwi .nl>
                        wrote:
                        >In article <fhi3o5$cqg$1@r egistered.motza rella.orgsantos h <santosh.k83@gm ail.comwrites:
                        In article
                        <59873ca2-b9a9-4e2a-87a9-4d0cc4844b4f@e1 0g2000prf.googl egroups.com>,
                        sophia.agnes@gm ail.com <sophia.agnes@g mail.comwrote on Thursday 15
                        Nov 2007 11:52 pm:
                        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.
                        >
                        >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.
                        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.
                        --
                        Al Balmer
                        Sun City, AZ

                        Comment

                        • Richard Heathfield

                          #13
                          Re: C doubts

                          Szabolcs Nagy said:

                          <snip>
                          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.

                          --
                          Richard Heathfield <http://www.cpax.org.uk >
                          Email: -http://www. +rjh@
                          Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                          "Usenet is a strange place" - dmr 29 July 1999

                          Comment

                          • Chris Dollin

                            #14
                            Re: C doubts

                            sophia.agnes@gm ail.com wrote:
                            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
                            Dik and Al have mentioned a paper showing how to de-goto a program;
                            I'd just like to add what I found an illuminating tactic in understanding
                            the possibilities of goto-removal.

                            You can /clearly/ rewrite a [non-threaded] program into a single loop,
                            containing a single switch, using a single switch index variable; each
                            case of the switch does some work and then updates the index to refer
                            to the next case to execute.

                            We do this rewriting all the time: it's called "compilatio n", and the
                            loop-and-switch is encoded in your processor.

                            This shows [informally] that (a) you can always de-goto a [threadless]
                            program, (b) that a formal proof, while satisfying, may not really be
                            saying very much, and (c) just because some code is "structured " in
                            terms of while/if-switch/sequence doesn't mean its /good/.

                            --
                            Chris "IMAO" Dollin

                            Hewlett-Packard Limited registered no:
                            registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

                            Comment

                            • dj3vande@csclub.uwaterloo.ca.invalid

                              #15
                              Re: C doubts

                              In article <fhjo8r$51v$1@t adcaster.hpl.hp .com>,
                              Chris Dollin <chris.dollin@h p.comwrote:
                              >Dik and Al have mentioned a paper showing how to de-goto a program;
                              >I'd just like to add what I found an illuminating tactic in understanding
                              >the possibilities of goto-removal.
                              >
                              >You can /clearly/ rewrite a [non-threaded] program into a single loop,
                              >containing a single switch, using a single switch index variable; each
                              >case of the switch does some work and then updates the index to refer
                              >to the next case to execute.
                              >
                              >We do this rewriting all the time: it's called "compilatio n", and the
                              >loop-and-switch is encoded in your processor.
                              Alternatively, ask a nearby functional programmer to explain
                              continuation-passing style. (This is especially good if you're the
                              sort of person who likes having your brain explode.)

                              Having sort of wrapped my head around how that works is even turning
                              out to be useful for C programming of a less pathological sort than
                              removing gotos[1], since the GUI toolkit I'm trying to figure out how
                              to use[2] doesn't appear to support modal loops, so I'm having to write
                              operations requiring user interaction as chained callbacks in CPS
                              form: Set up a widget, attach a callback for the next step in the
                              process, go back to the main event loop and let the widget I just set
                              up invoke the callback when the user has reacted appropriately. (Or,
                              at least, that's the first way I came up with to make it work. There
                              may very well be an obvious and easy way to do what I'm trying to
                              accomplish that requires a little bit less of a bent mind to understand
                              it.)


                              dave

                              [1] Though I've heard rumours that there are people who would not
                              exactly describe Xt as "less pathological" than anything this side
                              of INTERCAL.
                              [2] ObC: It's implemented as a C API. Actually, it could be
                              (and, if I'm not mistaken, is or is mostly) implemented entirely in
                              C-plus-calls-to-a-lower-level-API-also-with-C-bindings, and the
                              nonportable bits of the whole thing end up being reducible to
                              not much more than pushing data through a socket.

                              Comment

                              Working...