any error in the code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joe Wright

    #16
    Re: any error in the code

    Richard Heathfield wrote:
    Bartc said:
    >
    >Richard Heathfield wrote:
    >>
    >>Well, you're right - I'm forever forgetting to #include <string.h>,
    >>and being bluntly reminded of the fact only when I compile the code
    >>on a Win32 box
    >What's the disadvantage of just including *all* of the standard C headers
    >(or more likely creating a single header file that includes them)?
    >
    Partly, it increases compilation time. But I think most people's *real*
    objection to it is that it's inelegant.
    >
    Yes, it's inelegant. But why does it increase compilation time? The time
    required to read the headers must be too trivial to be calculated and
    included in 'compilation time'. Or not?

    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---

    Comment

    • Ian Collins

      #17
      Re: any error in the code

      Joe Wright wrote:
      Richard Heathfield wrote:
      >Bartc said:
      >>
      >>Richard Heathfield wrote:
      >>>
      >>>Well, you're right - I'm forever forgetting to #include <string.h>,
      >>>and being bluntly reminded of the fact only when I compile the code
      >>>on a Win32 box
      >>What's the disadvantage of just including *all* of the standard C
      >>headers
      >>(or more likely creating a single header file that includes them)?
      >>
      >Partly, it increases compilation time. But I think most people's
      >*real* objection to it is that it's inelegant.
      >>
      >
      Yes, it's inelegant. But why does it increase compilation time? The time
      required to read the headers must be too trivial to be calculated and
      included in 'compilation time'. Or not?
      >
      It may even improve compile time if the compiler supports precompiled
      headers.

      --
      Ian Collins.

      Comment

      • Jack Klein

        #18
        Re: any error in the code

        On Tue, 1 Apr 2008 12:11:44 -0400, lawrence.jones@ siemens.com wrote in
        comp.lang.c:
        Jack.Thomson.v3 @gmail.com wrote:

        Always free the dynamically allocated memory before exitting from the
        program
        >
        Why? Any properly functioning OS will do that automatically after the
        program exits, usually much more efficiently than the program can.
        >
        -Larry Jones
        Ahem.

        Perhaps because the C standard does not, and cannot, impose
        requirements on operating systems. So the C standard neither
        guarantees nor even specifies what the result on the platform might be
        if a C executable terminates without releasing dynamically allocated
        memory.

        And in the real world, at least some early versions of MS-DOS were
        prone to displaying an error message along the lines of "Memory Arena
        Corrupted, System Halted" and locking up if programs exited without
        releasing allocated memory.

        I wouldn't be surprised if there were some oddball niche operating
        systems around today that might have problems, even if popular desktop
        OS's handle the situation.

        --
        Jack Klein
        Home: http://JK-Technology.Com
        FAQs for
        comp.lang.c http://c-faq.com/
        comp.lang.c++ http://www.parashift.com/c++-faq-lite/
        alt.comp.lang.l earn.c-c++

        Comment

        • Richard Heathfield

          #19
          Re: any error in the code

          Harald van D?k said:

          <snip>
          However, even then it's possible to disable this by adding -fno-builtin
          to the compiler options.
          ....which is all completely off-topic here, of course (but that hasn't
          stopped me from gratefully adding -fno-builtin to my makefile generator).

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

          • parag_paul@hotmail.com

            #20
            Re: any error in the code

            On Apr 2, 11:14 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
            Harald van D?k said:
            >
            <snip>
            >
            However, even then it's possible to disable this by adding -fno-builtin
            to the compiler options.
            >
            ...which is all completely off-topic here, of course (but that hasn't
            stopped me from gratefully adding -fno-builtin to my makefile generator).
            >
            --
            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
            HI All
            Thank you all for letting so many views out on this .

            I also wanted to know, whether there is any error in the way the
            function Error is defined. Well, if it exists from this function , it
            definitely measn that we dont need to free anything , do we>

            Even after the returned value is a NULL;
            I am giving the full code here
            #include <stdlib.h>
            #include <stdio.h>
            void Error(char* s)
            {
            printf(s);
            return;
            }

            int main()
            {
            int *p;
            p = (int*)malloc(si zeof(int));
            if(p == NULL)
            {
            Error("Could not allocate the memory\n");
            Error("Quitting ....\n");
            exit(1);
            }
            else
            {
            /*some stuff to use p*/
            }
            return 0;
            }

            Comment

            • Richard Heathfield

              #21
              Re: any error in the code

              parag_paul@hotm ail.com said:

              <snip>
              I also wanted to know, whether there is any error in the way the
              function Error is defined. Well, if it exists from this function , it
              definitely measn that we dont need to free anything , do we>
              >
              Even after the returned value is a NULL;
              I am giving the full code here
              #include <stdlib.h>
              #include <stdio.h>
              void Error(char* s)
              {
              printf(s);
              return;
              }
              Careful. Think about what printf will think, if s contains any % signs!

              What do you think of this replacement?

              void error(FILE *fp, const char *s)
              {
              fprintf(fp, "Error: %s\n", s);
              }
              int main()
              {
              int *p;
              p = (int*)malloc(si zeof(int));
              p = malloc(sizeof *p);

              But why allocate memory for a single int? Wouldn't it be easier to do:

              int i;
              if(p == NULL)
              {
              Error("Could not allocate the memory\n");
              Error("Quitting ....\n");
              exit(1);
              exit(EXIT_FAILU RE);
              }
              else
              {
              /*some stuff to use p*/
              ....followed by free(p);
              }
              return 0;
              }
              --
              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

              • CBFalconer

                #22
                Re: any error in the code

                "parag_paul@hot mail.com" wrote:
                >
                int main() {
                should read "int main(void) {".
                int *p;
                p = (int*)malloc(si zeof(int));
                should have no cast. The cast is not needed, and is hiding the
                critical error of failing to #include <stdlib.h>
                if (p == NULL) {
                Error("Could not allocate the memory\n");
                Error("Quitting ....\n");
                exit(1);
                The functions "Error" and "exit" are not declared. Error is not a
                standard C function. exit is accessed via #include <stdlib.h>.
                The argument '1' for it is not legitimate, the only legal arguments
                are 0, EXIT_SUCCESS and EXIT_FAILURE. Again, the EXIT_* values are
                declared in <stdlib.h>
                }
                Here you are missing a "return 0". main returns an int (same as
                exit). Do so.

                --
                [mail]: Chuck F (cbfalconer at maineline dot net)
                [page]: <http://cbfalconer.home .att.net>
                Try the download section.



                --
                Posted via a free Usenet account from http://www.teranews.com

                Comment

                • Richard Tobin

                  #23
                  Re: any error in the code

                  In article <8dt5v312qia63j q10oaoubtjj2fse ibrje@4ax.com>,
                  >Why? Any properly functioning OS will do that automatically after the
                  >program exits, usually much more efficiently than the program can.
                  >Perhaps because the C standard does not, and cannot, impose
                  >requirements on operating systems. So the C standard neither
                  >guarantees nor even specifies what the result on the platform might be
                  >if a C executable terminates without releasing dynamically allocated
                  >memory.
                  If it can't impose requirements on operating systems, it can't specify
                  what the result might be if a program terminates after releasing
                  dynamically allocated memory.

                  -- Richard
                  --
                  :wq

                  Comment

                  • Bartc

                    #24
                    Re: any error in the code

                    Joe Wright wrote:
                    Richard Heathfield wrote:
                    >Bartc said:
                    >>
                    >>Richard Heathfield wrote:
                    >>>
                    >>>Well, you're right - I'm forever forgetting to #include <string.h>,
                    >>>and being bluntly reminded of the fact only when I compile the code
                    >>>on a Win32 box
                    >>What's the disadvantage of just including *all* of the standard C
                    >>headers (or more likely creating a single header file that includes
                    >>them)?
                    >>
                    >Partly, it increases compilation time. But I think most people's
                    >*real* objection to it is that it's inelegant.
                    >>
                    >
                    Yes, it's inelegant. But why does it increase compilation time? The
                    time required to read the headers must be too trivial to be
                    calculated and included in 'compilation time'. Or not?
                    I'm now using #include "stdhdr.h", which includes all 24 C99 headers, and
                    the compilation time is still pretty much zero for an empty program.

                    Total about 3000 lines, compared with some 20000 lines for Windows header
                    files for example.

                    Only problem is, when posting code, I need to pick and choose the include
                    files that are used. Now why didn't something like my stdhdr.h get into the
                    standard?

                    And I think my idea is much more elegant than having having some arbitrary,
                    and slightly different, subset of the 24 headers at the start of every
                    module, to add to the maintenance work.

                    --
                    Bart


                    Comment

                    • Joachim Schmitz

                      #25
                      Re: any error in the code

                      Nick Keighley wrote:
                      On 2 Apr, 08:29, "parag_p...@hot mail.com" <parag_p...@hot mail.com>
                      wrote:
                      >On Apr 2, 11:14 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
                      >
                      <snip>
                      >{
                      >printf(s);
                      >
                      this function seems to do almost nothing. It could give
                      odd behaviour if s contained any % symbols. Use
                      >
                      printf "%s\n", s);
                      rather
                      printf("%s\n", s);
                      but better
                      fprintf(stderr, "%s\n", s);
                      After all it is supposed to print en error message

                      Bye, Jojo


                      Comment

                      • parag_paul@hotmail.com

                        #26
                        Re: any error in the code

                        On Apr 2, 5:56 pm, "Joachim Schmitz" <nospam.j...@sc hmitz-digital.de>
                        wrote:
                        Nick Keighley wrote:
                        On 2 Apr, 08:29, "parag_p...@hot mail.com" <parag_p...@hot mail.com>
                        wrote:
                        On Apr 2, 11:14 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
                        >
                         <snip>
                        {
                        printf(s);
                        >
                        this function seems to do almost nothing. It could give
                        odd behaviour if s contained any % symbols. Use
                        >
                            printf "%s\n", s);
                        >
                        rather
                              printf("%s\n", s);
                        but better
                              fprintf(stderr, "%s\n", s);
                        After all it is supposed to print en error message
                        >
                        Bye, Jojo
                        Thank you all,

                        It was a valuable learning experience. But does including strhdr.h
                        make the compilation procedure costlier

                        or the compiler has any optimizations like the
                        #ifndef __STDHDR.H__

                        -Parag

                        Comment

                        • santosh

                          #27
                          Re: any error in the code

                          parag_paul@hotm ail.com wrote:
                          On Apr 2, 5:56 pm, "Joachim Schmitz" <nospam.j...@sc hmitz-digital.de>
                          wrote:
                          >Nick Keighley wrote:
                          On 2 Apr, 08:29, "parag_p...@hot mail.com" <parag_p...@hot mail.com>
                          wrote:
                          >On Apr 2, 11:14 am, Richard Heathfield <r...@see.sig.i nvalid>
                          >wrote:
                          >>
                          ><snip>
                          >{
                          >printf(s);
                          >>
                          this function seems to do almost nothing. It could give
                          odd behaviour if s contained any % symbols. Use
                          >>
                          printf "%s\n", s);
                          >>
                          >rather
                          >printf("%s\n ", s);
                          >but better
                          >fprintf(stderr ,"%s\n", s);
                          >After all it is supposed to print en error message
                          >>
                          >Bye, Jojo
                          >
                          Thank you all,
                          >
                          It was a valuable learning experience. But does including strhdr.h
                          make the compilation procedure costlier
                          >
                          or the compiler has any optimizations like the
                          #ifndef __STDHDR.H__
                          No standard header includes any other standard header, and yes,
                          your "stdhdrs.h" should probably use the customary include guard.

                          Comment

                          Working...