right padding

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phil Carmody

    #46
    Re: right padding

    Pilcrow <Pilcrow6@gmail .comwrites:
    On Wed, 29 Oct 2008 19:42:23 +0200, Phil Carmody
    <thefatphil_dem unged@yahoo.co. ukwrote:
    >
    >>Pilcrow <Pilcrow6@gmail .comwrites:
    >>char *pad( /* func returns ptr to start of string */
    >> int n, /* number to print */
    >> size_t width, /* pad to this width */
    >> int padch, /* character to pad with */
    >> int rl) /* pad on right (0) or left (1) */
    >>{
    >> char *s; /* ptr to output string */
    >> int count = 0; /* count of characters */
    >> int dig = digits(n); /* space required for the number */
    >> if(dig width) {
    >> fputs("Field not big enough for data", stderr);
    >> exit (EXIT_FAILURE);
    >> }
    >>>
    >> free(s);
    >>
    >>I sense a demonsneeze coming.
    >
    Your remarks are always so helpful and so clearly expressed! I always
    wait for your input.
    Well you should be idly waiting, you should be reading and learning.

    Google for "demons fly out of your nose", and also read all the
    other responses correcting you in this thread. If you refuse to
    learn, there's no point trying to teach you.

    Phil
    --
    Christianity has such a contemptible opinion of human nature that it does
    not believe a man can tell the truth unless frightened by a belief in God.
    No lower opinion of the human race has ever been expressed.
    -- Robert Green Ingersoll (1833-1899), American politician and orator
    (Thank you for the correction. See - learning from others is easy!)

    Comment

    • Pilcrow

      #47
      Re: right padding

      On Wed, 29 Oct 2008 22:29:10 +0200, Phil Carmody
      <thefatphil_dem unged@yahoo.co. ukwrote:
      >Pilcrow <Pilcrow6@gmail .comwrites:
      >On Wed, 29 Oct 2008 19:42:23 +0200, Phil Carmody
      ><thefatphil_de munged@yahoo.co .ukwrote:
      >>
      >>>Pilcrow <Pilcrow6@gmail .comwrites:
      >>>char *pad( /* func returns ptr to start of string */
      >>> int n, /* number to print */
      >>> size_t width, /* pad to this width */
      >>> int padch, /* character to pad with */
      >>> int rl) /* pad on right (0) or left (1) */
      >>>{
      >>> char *s; /* ptr to output string */
      >>> int count = 0; /* count of characters */
      >>> int dig = digits(n); /* space required for the number */
      >>> if(dig width) {
      >>> fputs("Field not big enough for data", stderr);
      >>> exit (EXIT_FAILURE);
      >>> }
      >>>>
      >>> free(s);
      >>>
      >>>I sense a demonsneeze coming.
      >>
      >Your remarks are always so helpful and so clearly expressed! I always
      >wait for your input.
      >
      >Well you should be idly waiting, you should be reading and learning.
      >
      >Google for "demons fly out of your nose", and also read all the
      >other responses correcting you in this thread. If you refuse to
      >learn, there's no point trying to teach you.
      >
      >Phil
      <http://www.urbandictio nary.com/define.php?term =demonsneeze>
      <http://www.urbandictio nary.com/define.php?term =Demonophobia>
      <http://www.noisebot.co m/images/darkside2-thumb.gif>
      <http://www.noisebot.co m/images/roundhouse-thumb.gif>

      Want to buy a duck?


      Comment

      • Pilcrow

        #48
        Re: right padding

        On Wed, 29 Oct 2008 11:37:18 -0700 (PDT), jameskuyper
        <jameskuyper@ve rizon.netwrote:
        >Pilcrow wrote:
        >On Wed, 29 Oct 2008 11:14:27 +0000, Ben Bacarisse <ben.usenet@bsb .me.uk>
        >wrote:
        >[snip]
        >No, sorry, but I am right about this. Inside pad, the call to free is
        >an error. You have, in essence, this code:
        >
        char *s;
        /* code that does not set s */
        free(s);
        >
        >This is undefined behaviour because s is indeterminate.
        >...
        >Well, did some tests. Am now really confused. I hope somebody can
        >explain this?!
        >
        >Well, that's simple. The behavior of your program is undefined. As a
        >result, there is nothing that counts as incorrect output from your
        >program. It could print out the text of "Hamlet", and that would be
        >correct output. Only when you've fixed the bug that has been
        >repeatedly brought to your attention will it be possible to talk
        >meaningfully about what the behavior of your program should be.
        >
        >...
        >{
        > char *s; /* ptr to output string */
        >
        >'s' has not been initialized here. As a result, it's value is
        >indterminate .
        >
        > int count = 0; /* count of characters */
        > int dig = digits(n); /* space required for the number */
        > if(dig width) {
        > fputs("Field not big enough for data", stderr);
        > exit (EXIT_FAILURE);
        > }
        >
        >'s' has still not been given a value.
        >
        > free(s);
        >
        >One of the possibilities for objects with an indeterminate value is
        >that they may contain a trap representation. If that's the case, any
        >use of the value of 's' has undefined behavior, regardless of whether
        >it is passed to free(). Even if 's' doesn't have a trap
        >representation , you're still passing a value to free() about which you
        >know absolutely nothing. You should never pass a value to free() which
        >was not returned by either malloc(), calloc(), or realloc(); if you
        >pass it any other value, the behavior is undefined. You have no way of
        >knowing what value is being passed to free(), which means it's
        >overwhelming ly likely to be a value that cannot be safely passed to
        >free().
        Thank you very much. I have learned. Fixed code.

        --
        You are a gentleman and a scholar
        and a judge of fine whiskey.

        Comment

        • jameskuyper

          #49
          Re: right padding

          Pilcrow wrote:
          ....
          ... criticizing others for their rudeness is, in itself, rude.
          Miss Manners (Judith Martin) 2008-11-05: "It is not uncommon for rude
          people to act offended when their rudeness is not tolerated. Miss
          Manners assures you that this does not make it rude to refule to
          tolerate rudeness, so long as this is not done with retaliatory
          rudeness."

          I don't think Ben's remarks can be described as having been done "with
          retaliatory rudeness". He just said " I don't know if I am one of the
          people you are being rude about,".

          Comment

          • Tim Rentsch

            #50
            Re: right padding

            Richard Heathfield <rjh@see.sig.in validwrites:
            Pilcrow said:
            >
            free(s);
            s = malloc(width+1) ;
            assert(s != NULL);
            >
            Bad idea. Use assertions only when the program is incorrectly written.
            Using assertions inappropriately means the program is incorrectly
            written, wouldn't you say? ;) ;) ;)

            Comment

            • Stephen Sprunk

              #51
              Re: right padding

              Richard Heathfield wrote:
              Pilcrow said:
              >free(s);
              >s = malloc(width+1) ;
              >assert(s != NULL);
              >
              Bad idea. Use assertions only when the program is incorrectly written.
              Correction: assert() is for detecting "impossible " conditions that can
              only happen due to bugs. It is _not_ for handling normal error
              conditions, because that error handling will disappear when you've
              disabled assertions.

              S

              Comment

              • Richard Heathfield

                #52
                Re: right padding

                Tim Rentsch said:
                Richard Heathfield <rjh@see.sig.in validwrites:
                >
                >Pilcrow said:
                >>
                free(s);
                s = malloc(width+1) ;
                assert(s != NULL);
                >>
                >Bad idea. Use assertions only when the program is incorrectly written.
                >
                Using assertions inappropriately means the program is incorrectly
                written, wouldn't you say? ;) ;) ;)
                Aye! Lousy wording, wasn't it? I should have written something like:

                Use assertions only to test assumptions in your code that must be true if
                the program is correctly written. For example, if a particular function is
                inside a data "firewall" such that it is *impossible* for it to be called
                with invalid data, then it's okay (and in fact a good idea) to write
                something like:

                assert(temperat ure MELTING_POINT &&
                temperature < BOILING_POINT);

                What we're actually testing here is not the data, but the "firewall".

                The assertion would be inappropriate, however, in the "firewall" code
                itself. For example, if a user has just typed this stuff in, you wouldn't
                want to bomb out if he typo'd; you'd just want to prompt him with an error
                message and a request to re-enter the data.

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

                • William Pursell

                  #53
                  Re: right padding

                  On 28 Oct, 15:23, Pilcrow <Pilcr...@gmail .comwrote:
                  >
                  In the function, as written, I cannot see how the user can free the
                  buffer, since it is internal to the function.  I have, however, dealt
                  with the problem of ever-increasing memory allocation by saying:
                  --------------------------------------------------------------------
                  free(s);
                  s = malloc(width+1) ;
                  assert(s != NULL);

                  I don't think Richard's response to this was adequate, so I'm
                  going to say it more clearly.

                  DO NOT DO THAT.

                  This is absolutely the wrong way to use assert. Using assert
                  this way means that you believe it is logically impossible
                  for malloc to return NULL. I hope you don't believe that.

                  Comment

                  Working...