struct interdependencies

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Russell Shaw

    #16
    Re: struct interdependenci es

    Chris Croughton wrote:[color=blue]
    > On Mon, 18 Apr 2005 11:40:30 +1000, Russell Shaw
    > <rjshawN_o@s_pa m.netspace.net. au> wrote:
    >
    >[color=green]
    >>Old Wolf wrote:
    >>[color=darkred]
    >>>Chris Torek wrote:
    >>>
    >>>
    >>>>Stop using typedefs.
    >>>
    >>>One problem I find when using struct tags rather than typedefs:
    >>>
    >>> struct gargleblaster
    >>> {
    >>> int whatever;
    >>> };
    >>>
    >>> void foo(struct garbleglaster *ptr);
    >>>
    >>>will raise no compiler errors, until later on when I try to
    >>>define foo() and assign ptr, then it will tell me that ptr's
    >>>type is incompatible with what I'm assigning it to. Whereas:
    >>>
    >>> typedef struct { int whatever; } hyperspacebypas s;
    >>> void bar(hypespaceby pass *ptr);
    >>>
    >>>will cause a compiler error right then and there.[/color]
    >>
    >>Can't see why the latter should be in error. I was always using the
    >>same thing in gcc.[/color]
    >
    > Did you spot the error in the first example using struct? The error is
    > the same (mistyped identifier), but in the first case the compiler will
    > just say "OK, I'm using a structure with incomplete type, no problem"
    > until you try to access something in that structure or pass a pointer to
    > a structure with the correct name to the function. And then it can take
    > ages to spot cause of the error, because it's subtle and removed from
    > the place of the actual error. That's why using the typedef is better,
    > the compiler will spot immediately "hypespacebypas s is not a type".[/color]

    I have an annoying problem of reading by patterns, and if the patterns
    look similar, i don't see the problem even after reading multiple times.
    If i *know* the error is in that location, i start reading letter by letter
    (i got aspergers a bit).

    Comment

    • Chris Croughton

      #17
      Re: struct interdependenci es

      On Mon, 18 Apr 2005 21:14:39 +1000, Russell Shaw
      <rjshawN_o@s_pa m.netspace.net. au> wrote:
      [color=blue]
      > Chris Croughton wrote:[color=green]
      >> On Mon, 18 Apr 2005 11:40:30 +1000, Russell Shaw
      >> <rjshawN_o@s_pa m.netspace.net. au> wrote:
      >>[color=darkred]
      >>>Old Wolf wrote:
      >>>
      >>>>Chris Torek wrote:
      >>>>
      >>>>>Stop using typedefs.
      >>>>
      >>>>One problem I find when using struct tags rather than typedefs:
      >>>>
      >>>> struct gargleblaster
      >>>> {
      >>>> int whatever;
      >>>> };
      >>>>
      >>>> void foo(struct garbleglaster *ptr);
      >>>>
      >>>>will raise no compiler errors, until later on when I try to
      >>>>define foo() and assign ptr, then it will tell me that ptr's
      >>>>type is incompatible with what I'm assigning it to. Whereas:
      >>>>
      >>>> typedef struct { int whatever; } hyperspacebypas s;
      >>>> void bar(hypespaceby pass *ptr);
      >>>>
      >>>>will cause a compiler error right then and there.
      >>>
      >>>Can't see why the latter should be in error. I was always using the
      >>>same thing in gcc.[/color]
      >>
      >> Did you spot the error in the first example using struct? The error is
      >> the same (mistyped identifier), but in the first case the compiler will
      >> just say "OK, I'm using a structure with incomplete type, no problem"
      >> until you try to access something in that structure or pass a pointer to
      >> a structure with the correct name to the function. And then it can take
      >> ages to spot cause of the error, because it's subtle and removed from
      >> the place of the actual error. That's why using the typedef is better,
      >> the compiler will spot immediately "hypespacebypas s is not a type".[/color]
      >
      > I have an annoying problem of reading by patterns, and if the patterns
      > look similar, i don't see the problem even after reading multiple times.
      > If i *know* the error is in that location, i start reading letter by letter
      > (i got aspergers a bit).[/color]

      In this case your 'problem'[1] illustrated the reason for using the
      typedef form, because that will show you the line where the error is.
      If the error is shown only when a field is accessed or the function is
      called with the correct pointer, which might be several hundred lines
      further on (and not even in the same file, the prototype might be in a
      header). (I also didn't spot it right away, so I shoved it into a file
      and gcc pointed it out. I've also got a bit of (undiagnosed) Aspergers,
      and some dyslexia which has the 'i18n' problem, I see the start and end
      of a word and fill in the rest...)

      [1] I think everyone does that to an extent, does anyone actually read
      through all of the letters of every word they see? I only do it if I
      think something's wrong.

      Chris C

      Comment

      • Michael Wojcik

        #18
        Re: struct interdependenci es


        In article <1113777051.431 774.73200@l41g2 000cwc.googlegr oups.com>, "Old Wolf" <oldwolf@inspir e.net.nz> writes:[color=blue]
        >
        > One problem I find when using struct tags rather than typedefs:
        >
        > struct gargleblaster
        > {
        > int whatever;
        > };
        >
        > void foo(struct garbleglaster *ptr);
        >
        > will raise no compiler errors,[/color]

        It produces a diagnostic from at least one implementation I've used
        (I think it was one of Sun's C compilers), warning that I'd just
        declared an incomplete struct type in prototype scope, which was
        probably not what I wanted to do. A type declaration in prototype
        scope is useless, AFAICT, so that's a reasonable warning.
        [color=blue]
        > until later on when I try to
        > define foo() and assign ptr, then it will tell me that ptr's
        > type is incompatible with what I'm assigning it to.[/color]

        Still, isn't that a pretty clear diagnosis of the problem?

        --
        Michael Wojcik michael.wojcik@ microfocus.com

        ¡Peligro! ¡Hay llamas!

        Comment

        • Christian Bau

          #19
          Re: struct interdependenci es

          In article <d41ch6016nv@ne ws4.newsguy.com >,
          mwojcik@newsguy .com (Michael Wojcik) wrote:
          [color=blue]
          > In article <1113777051.431 774.73200@l41g2 000cwc.googlegr oups.com>, "Old Wolf"
          > <oldwolf@inspir e.net.nz> writes:[color=green]
          > >
          > > One problem I find when using struct tags rather than typedefs:
          > >
          > > struct gargleblaster
          > > {
          > > int whatever;
          > > };
          > >
          > > void foo(struct garbleglaster *ptr);
          > >
          > > will raise no compiler errors,[/color]
          >
          > It produces a diagnostic from at least one implementation I've used
          > (I think it was one of Sun's C compilers), warning that I'd just
          > declared an incomplete struct type in prototype scope, which was
          > probably not what I wanted to do. A type declaration in prototype
          > scope is useless, AFAICT, so that's a reasonable warning.[/color]

          Not really reasonable. Declaring a complete struct type in prototype
          code would be nonsense, because that declaration wouldn't be usable
          outside. But declaring a pointer to an incomplete struct is useful.
          There are even practical uses if the complete struct is never defined.

          Comment

          • Michael Wojcik

            #20
            Re: struct interdependenci es


            In article <christian.ba u-721E40.00200619 042005@slb-newsm1.svr.pol. co.uk>, Christian Bau <christian.bau@ cbau.freeserve. co.uk> writes:[color=blue]
            > In article <d41ch6016nv@ne ws4.newsguy.com >,
            > mwojcik@newsguy .com (Michael Wojcik) wrote:[color=green]
            > > In article <1113777051.431 774.73200@l41g2 000cwc.googlegr oups.com>, "Old Wolf"
            > > <oldwolf@inspir e.net.nz> writes:[color=darkred]
            > > >
            > > > One problem I find when using struct tags rather than typedefs:
            > > >
            > > > struct gargleblaster
            > > > {
            > > > int whatever;
            > > > };
            > > >
            > > > void foo(struct garbleglaster *ptr);
            > > >
            > > > will raise no compiler errors,[/color]
            > >
            > > It produces a diagnostic from at least one implementation I've used
            > > (I think it was one of Sun's C compilers), warning that I'd just
            > > declared an incomplete struct type in prototype scope, which was
            > > probably not what I wanted to do. A type declaration in prototype
            > > scope is useless, AFAICT, so that's a reasonable warning.[/color]
            >
            > Not really reasonable. Declaring a complete struct type in prototype
            > code would be nonsense,[/color]

            It's not nonsense; it's well-defined. It's just useless.
            [color=blue]
            > because that declaration wouldn't be usable
            > outside. But declaring a pointer to an incomplete struct is useful.
            > There are even practical uses if the complete struct is never defined.[/color]

            Indeed it is, and that doesn't involve a declaration of an incomplete
            struct type in prototype scope, provided the incomplete struct type
            has already been declared. The implementation in question does not
            issue a diagnostic for that, and I wasn't suggesting any implementa-
            tion should. It's a completely different situation.

            Note that if no declaration (complete or incomplete) of the struct
            type is in scope when the struct pointer type is used in the
            prototype, that still constitutes a declaration at prototype scope,
            which is *not* compatible with any subsequent declaration of a
            struct type with the same tag.

            That is,

            struct foo;
            void bar(struct foo *);
            void baz(struct foo *);

            correctly declares bar and baz as taking pointers to the same
            incomplete structure, but without a prior file-scope declaration of
            struct foo, their parameters would be of different type - and it
            would be a type that could not be replicated elsewhere. (The only
            place it could be used again is in the same prototype, which would
            still be pointless.)

            Prototype-scope declarations of struct types, complete or incomplete,
            are never useful, regardless of whether they're then used to declare
            pointer types.

            If you think a prototype-scope declaration of a struct type is ever
            useful, feel free to demonstrate how.

            --
            Michael Wojcik michael.wojcik@ microfocus.com

            Thus, the black lie, issuing from his base throat, becomes a boomerang to
            his hand, and he is hoist by his own petard, and finds himself a marked man.
            -- attributed to a "small-town newspaper editor in Wisconsin"

            Comment

            Working...