struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct));

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Torek

    #31
    Re: struct my_struct *p = (struct my_struct *)malloc(sizeof (struct my_struct));

    In article <GSCfc.89809$dP 1.269709@newsc. telia.net>
    Chris Fogelklou <chris.fogelklo u@comhem.se> writes:[color=blue]
    >Is there really that much wrong with making code compatible with both C and
    >C++? If we leave the cast in there, the code could be copied and pasted
    >into a C++ library verbatim. ...[/color]

    There is nothing inherently wrong with it, but I see it as a waste of
    effort in most cases.

    There are quite a few subtle differences between C and C++, so that
    one can write programs that are syntactically valid in both languages,
    compile in both languages, yet mean something different in each
    language. The problem is similar to getting Americans and Brits
    to understand each other: when an American committee "tables" a
    subject it is put aside to stop discussion, while the British
    committee "tables" the subject to bring it up and begin discussion.

    (I posted an example earlier today in which "sizeof type" changes
    when the code is compiled in the alternate language. Look for
    "struct scopetrick".)

    More typically, good C code often fails to compile at all as C++
    code (because -- among other things -- it omits casts that C++
    requires); and of course good C++ code generally uses "new" instead
    of malloc, or -- even better -- uses container classes.
    --
    In-Real-Life: Chris Torek, Wind River Systems
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: forget about it http://web.torek.net/torek/index.html
    Reading email is like searching for food in the garbage, thanks to spammers.

    Comment

    • CBFalconer

      #32
      Re: struct my_struct *p = (struct my_struct *)malloc(sizeof (structmy_struc t));

      Chris Torek wrote:[color=blue]
      > Chris Fogelklou <chris.fogelklo u@comhem.se> writes:
      >[color=green]
      >> Is there really that much wrong with making code compatible with
      >> both C and C++? If we leave the cast in there, the code could
      >> be copied and pasted into a C++ library verbatim. ...[/color]
      >
      > There is nothing inherently wrong with it, but I see it as a
      > waste of effort in most cases.
      >
      > There are quite a few subtle differences between C and C++, so
      > that one can write programs that are syntactically valid in both
      > languages, compile in both languages, yet mean something
      > different in each language. The problem is similar to getting
      > Americans and Brits to understand each other: when an American[/color]
      ....

      Back when I was young and in my prime there was this amiable, but
      poverty stricken Yank who met this foxy Brit chick. Nature took
      its course, but then she told him: "I am getting divorced, with a
      big settlement. Meet me after the divorce, but meanwhile I have
      to stay pristine." And she handed him an envelope and told him to
      open it after the day was over.

      Well, he followed instructions, and opened it and read: "Until
      7/6, noon, in Trafalger Square.". Since it was January, he went
      off and did other things, such as roping calves etc., and returned
      to London on 7/6. He never found her again.

      She meant every word of it, and she was there on 7/6, but never
      found him again.

      Your mission, should you care to accept it, is to explain this
      paradox. Alternate timelines or universes will not be accepted.

      --
      fix (vb.): 1. to paper over, obscure, hide from public view; 2.
      to work around, in a way that produces unintended consequences
      that are worse than the original problem. Usage: "Windows ME
      fixes many of the shortcomings of Windows 98 SE". - Hutchison


      Comment

      • Severian

        #33
        Re: struct my_struct *p = (struct my_struct *)malloc(sizeof (struct my_struct));

        On Fri, 16 Apr 2004 04:29:22 GMT, CBFalconer <cbfalconer@yah oo.com>
        wrote:
        [color=blue]
        >Chris Torek wrote:[color=green]
        >> Chris Fogelklou <chris.fogelklo u@comhem.se> writes:
        >>[color=darkred]
        >>> Is there really that much wrong with making code compatible with
        >>> both C and C++? If we leave the cast in there, the code could
        >>> be copied and pasted into a C++ library verbatim. ...[/color]
        >>
        >> There is nothing inherently wrong with it, but I see it as a
        >> waste of effort in most cases.
        >>
        >> There are quite a few subtle differences between C and C++, so
        >> that one can write programs that are syntactically valid in both
        >> languages, compile in both languages, yet mean something
        >> different in each language. The problem is similar to getting
        >> Americans and Brits to understand each other: when an American[/color]
        >...
        >
        >Back when I was young and in my prime there was this amiable, but
        >poverty stricken Yank who met this foxy Brit chick. Nature took
        >its course, but then she told him: "I am getting divorced, with a
        >big settlement. Meet me after the divorce, but meanwhile I have
        >to stay pristine." And she handed him an envelope and told him to
        >open it after the day was over.
        >
        >Well, he followed instructions, and opened it and read: "Until
        >7/6, noon, in Trafalger Square.". Since it was January, he went
        >off and did other things, such as roping calves etc., and returned
        >to London on 7/6. He never found her again.
        >
        >She meant every word of it, and she was there on 7/6, but never
        >found him again.
        >
        >Your mission, should you care to accept it, is to explain this
        >paradox. Alternate timelines or universes will not be accepted.[/color]

        Too easy. June 7th != July 6th. However, your language point is
        evident.

        --
        Sev

        Comment

        • Christian Bau

          #34
          Re: struct my_struct *p = (struct my_struct *)malloc(sizeof (struct my_struct));

          In article <9gou709pl8pkpa j8k8eni61qjkufd eihlj@4ax.com>,
          Severian <severian@chlam ydia-is-not-a-flower.com> wrote:
          [color=blue]
          > On Fri, 16 Apr 2004 04:29:22 GMT, CBFalconer <cbfalconer@yah oo.com>
          > wrote:
          >[color=green]
          > >Chris Torek wrote:[color=darkred]
          > >> Chris Fogelklou <chris.fogelklo u@comhem.se> writes:
          > >>
          > >>> Is there really that much wrong with making code compatible with
          > >>> both C and C++? If we leave the cast in there, the code could
          > >>> be copied and pasted into a C++ library verbatim. ...
          > >>
          > >> There is nothing inherently wrong with it, but I see it as a
          > >> waste of effort in most cases.
          > >>
          > >> There are quite a few subtle differences between C and C++, so
          > >> that one can write programs that are syntactically valid in both
          > >> languages, compile in both languages, yet mean something
          > >> different in each language. The problem is similar to getting
          > >> Americans and Brits to understand each other: when an American[/color]
          > >...
          > >
          > >Back when I was young and in my prime there was this amiable, but
          > >poverty stricken Yank who met this foxy Brit chick. Nature took
          > >its course, but then she told him: "I am getting divorced, with a
          > >big settlement. Meet me after the divorce, but meanwhile I have
          > >to stay pristine." And she handed him an envelope and told him to
          > >open it after the day was over.
          > >
          > >Well, he followed instructions, and opened it and read: "Until
          > >7/6, noon, in Trafalger Square.". Since it was January, he went
          > >off and did other things, such as roping calves etc., and returned
          > >to London on 7/6. He never found her again.
          > >
          > >She meant every word of it, and she was there on 7/6, but never
          > >found him again.
          > >
          > >Your mission, should you care to accept it, is to explain this
          > >paradox. Alternate timelines or universes will not be accepted.[/color]
          >
          > Too easy. June 7th != July 6th. However, your language point is
          > evident.[/color]

          Many years ago, a C++ framework that I investigated for its usefulness
          changed a constructor from

          Point::Point (int width, int height);

          to

          Point::Point (int vertical, int horizontal);

          I decided not to use that framework...

          Comment

          Working...