Flexible arrays - v confused

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

    #16
    Re: Flexible arrays - v confused

    Keith Thompson wrote:[color=blue]
    > websnarf@gmail. com writes:[color=green]
    > > Chris Torek wrote:[color=darkred]
    > >> #include <stdlib.h>
    > >> struct vector {
    > >> size_t n; /* number of values in the vector */
    > >> double val[1]; /* actually size n */
    > >> };
    > >>
    > >> struct vector *vec_new(size_t n) {
    > >> struct vector *p;
    > >>
    > >> /* need n-1 here because the array has an "extra" element */
    > >> p = malloc(sizeof *p + (n ? n - 1 : 0) * sizeof p->val[0]);[/color]
    > >
    > > I do this as follows:
    > >
    > > p = (struct vector *) malloc (offsetof (struct vector, val) +
    > > n * sizeof (p->val[0]));
    > >
    > > Which means I need a #include <stddef.h>[/color]
    >
    > Why do you uselessly cast the result of malloc()?[/color]

    Because I like to use automatic error detection tools when I program,
    and that includes using a C++ compiler (which has stricter type
    checking). It means I have to conceed to things like the above in
    order to gain the benefit of stronger type checking elsewhere in my
    programs.

    So if you take the opposite point of view, would it be fair to say that
    you avoid things like Lint and ignore warning messages coming from your
    compiler? Or are you saying that anything superfluous, like comments
    and whitespace, should be eradicated from code?

    --
    Paul Hsieh
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.



    Comment

    • Keith Thompson

      #17
      Re: Flexible arrays - v confused

      websnarf@gmail. com writes:[color=blue]
      > Keith Thompson wrote:[/color]
      [...][color=blue][color=green]
      >> Why do you uselessly cast the result of malloc()?[/color]
      >
      > Because I like to use automatic error detection tools when I program,
      > and that includes using a C++ compiler (which has stricter type
      > checking). It means I have to conceed to things like the above in
      > order to gain the benefit of stronger type checking elsewhere in my
      > programs.
      >
      > So if you take the opposite point of view, would it be fair to say that
      > you avoid things like Lint and ignore warning messages coming from your
      > compiler? Or are you saying that anything superfluous, like comments
      > and whitespace, should be eradicated from code?[/color]

      Yes, absolutely, because you're always right and anyone who disagrees
      with you must be a complete idiot. Thank you for bringing this to my
      attention.

      --
      Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
      San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
      We must do something. This is something. Therefore, we must do this.

      Comment

      • websnarf@gmail.com

        #18
        Re: Flexible arrays - v confused

        Keith Thompson wrote:[color=blue]
        > websnarf@gmail. com writes:[color=green]
        > > Keith Thompson wrote:[/color]
        > [...][color=green][color=darkred]
        > >> Why do you uselessly cast the result of malloc()?[/color]
        > >
        > > Because I like to use automatic error detection tools when I program,
        > > and that includes using a C++ compiler (which has stricter type
        > > checking). It means I have to conceed to things like the above in
        > > order to gain the benefit of stronger type checking elsewhere in my
        > > programs.
        > >
        > > So if you take the opposite point of view, would it be fair to say that
        > > you avoid things like Lint and ignore warning messages coming from your
        > > compiler? Or are you saying that anything superfluous, like comments
        > > and whitespace, should be eradicated from code?[/color]
        >
        > Yes, absolutely, because you're always right and anyone who disagrees
        > with you must be a complete idiot. Thank you for bringing this to my
        > attention.[/color]

        You intentionally instigated this. You used the language "uselessly"
        and your canned argument against this cast is well known to me, and you
        know that. So you respond with this question with the intention of
        laying this pathetic trap. But, if you've ever bothered to read my
        posts, you've known my counter argument before this post.

        My reducto ad absurdum in the second paragraph is meant to address the
        position "if you take the opposite point of view". If you understood
        what I was saying rather than taking an antagonistic-only stance you
        would have realized that what I was saying is that the problem is that
        you have not clarified the basis for your point of view, and unless its
        insane the ball is in your court to explain why you want to remove this
        "useless" cast.

        --
        Paul Hsieh
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.



        Comment

        • Richard Bos

          #19
          Re: Flexible arrays - v confused

          websnarf@gmail. com wrote:
          [color=blue]
          > Keith Thompson wrote:[color=green]
          > > websnarf@gmail. com writes:[color=darkred]
          > > > p = (struct vector *) malloc (offsetof (struct vector, val) +
          > > > n * sizeof (p->val[0]));
          > > >
          > > > Which means I need a #include <stddef.h>[/color]
          > >
          > > Why do you uselessly cast the result of malloc()?[/color]
          >
          > Because I like to use automatic error detection tools when I program,
          > and that includes using a C++ compiler (which has stricter type
          > checking). It means I have to conceed to things like the above in
          > order to gain the benefit of stronger type checking elsewhere in my
          > programs.[/color]

          So in order to gain a few dubious warnings which quite possibly won't
          even apply to the language you're writing in, you prevent your real
          compiler from spotting what would be a read and potentially dangerous
          mistake?

          Sheer brilliance.

          Richard

          Comment

          • websnarf@gmail.com

            #20
            Re: Flexible arrays - v confused

            Richard Bos wrote:[color=blue]
            > websnarf@gmail. com wrote:[color=green]
            > > Keith Thompson wrote:[color=darkred]
            > > > websnarf@gmail. com writes:
            > > > > p = (struct vector *) malloc (offsetof (struct vector, val) +
            > > > > n * sizeof (p->val[0]));
            > > > >
            > > > > Which means I need a #include <stddef.h>
            > > >
            > > > Why do you uselessly cast the result of malloc()?[/color]
            > >
            > > Because I like to use automatic error detection tools when I program,
            > > and that includes using a C++ compiler (which has stricter type
            > > checking). It means I have to conceed to things like the above in
            > > order to gain the benefit of stronger type checking elsewhere in my
            > > programs.[/color]
            >
            > So in order to gain a few dubious warnings which quite possibly won't
            > even apply to the language you're writing in, you prevent your real
            > compiler from spotting what would be a read and potentially dangerous
            > mistake?[/color]

            C++ compilers also enforce prototype predeclarations (so would C99
            compilers if they existed). So exactly which dangerous mistake do you
            think I am going to miss?

            --
            Paul Hsieh
            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.



            Comment

            • Dave Thompson

              #21
              Re: Flexible arrays - v confused

              On 17 Feb 2006 11:02:13 -0800, websnarf@gmail. com wrote:
              [color=blue]
              > Richard Bos wrote:[/color]
              (casting return of malloc)[color=blue]
              > C++ compilers also enforce prototype predeclarations (so would C99
              > compilers if they existed). So exactly which dangerous mistake do you
              > think I am going to miss?[/color]

              C++ does require prototypes (though not using that term).

              C99 requires _declarations_ but not necessarily prototypes. That is
              enough for the return type, as here, but not arguments.

              - David.Thompson1 at worldnet.att.ne t

              Comment

              Working...