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 Fogelklou

    #1

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

    What is wrong with the above?

    Don't worry, I already know (learned my lesson last week.) It is for the
    benefit of our resident compiler guru who seems to think you need the cast.
    I thought it too, up until I started posting here!

    Thanks,

    Chris

  • Chris Fogelklou

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

    "Chris Fogelklou" <chris.fogelklo u@comhem.se> wrote in message
    news:7lxfc.8975 8$dP1.269223@ne wsc.telia.net.. .[color=blue]
    > What is wrong with the above?
    >
    > Don't worry, I already know (learned my lesson last week.) It is for the
    > benefit of our resident compiler guru who seems to think you need the[/color]
    cast.[color=blue]
    > I thought it too, up until I started posting here!
    >
    > Thanks,
    >
    > Chris
    >[/color]
    Even better, here is the email itself. Please help!

    <start email>
    First, Casts from void * are necessary both in C and C++. This is from the
    same reason you told me: otherwise, if the compiler wouldn't
    know what is the size of the object which is pointed at, it might get
    confused with arithmetic operations. You can ask him where did he learned
    this rule from. tell me if you want me to correspond directly with him.

    Second, a possible reason to use integral types for addresses rather than
    void *, is that in order to perform bitwise operations on an
    address, you have to use operators of an integral type.
    <end email>

    Comment

    • Régis Troadec

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


      "Chris Fogelklou" <chris.fogelklo u@comhem.se> a écrit dans le message de
      news:Snxfc.8975 9$dP1.269223@ne wsc.telia.net.. .[color=blue]
      > "Chris Fogelklou" <chris.fogelklo u@comhem.se> wrote in message[/color]

      Hi,
      [color=blue]
      > news:7lxfc.8975 8$dP1.269223@ne wsc.telia.net.. .[color=green]
      > > What is wrong with the above?
      > >
      > > Don't worry, I already know (learned my lesson last week.) It is for[/color][/color]
      the[color=blue][color=green]
      > > benefit of our resident compiler guru who seems to think you need the[/color]
      > cast.[color=green]
      > > I thought it too, up until I started posting here!
      > >
      > > Thanks,
      > >
      > > Chris
      > >[/color]
      > Even better, here is the email itself. Please help!
      >
      > <start email>
      > First, Casts from void * are necessary both in C and C++. This is from the[/color]

      Casts from void* are necessary in C++, not in C.
      [color=blue]
      > same reason you told me: otherwise, if the compiler wouldn't
      > know what is the size of the object which is pointed at, it might get
      > confused with arithmetic operations. You can ask him where did he learned
      > this rule from. tell me if you want me to correspond directly with him.
      >
      > Second, a possible reason to use integral types for addresses rather than
      > void *, is that in order to perform bitwise operations on an
      > address, you have to use operators of an integral type.
      > <end email>
      >[/color]

      Regis


      Comment

      • Chris Fogelklou

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


        "Régis Troadec" <regt@wanadoo.f r> wrote in message
        news:c5m8hk$hc5 $1@news-reader3.wanadoo .fr...[color=blue]
        >
        > "Chris Fogelklou" <chris.fogelklo u@comhem.se> a écrit dans le message de
        > news:Snxfc.8975 9$dP1.269223@ne wsc.telia.net.. .[color=green]
        > > "Chris Fogelklou" <chris.fogelklo u@comhem.se> wrote in message[/color]
        >
        > Hi,
        >[color=green]
        > > news:7lxfc.8975 8$dP1.269223@ne wsc.telia.net.. .[color=darkred]
        > > > What is wrong with the above?
        > > >
        > > > Don't worry, I already know (learned my lesson last week.) It is for[/color][/color]
        > the[color=green][color=darkred]
        > > > benefit of our resident compiler guru who seems to think you need the[/color]
        > > cast.[color=darkred]
        > > > I thought it too, up until I started posting here!
        > > >
        > > > Thanks,
        > > >
        > > > Chris
        > > >[/color]
        > > Even better, here is the email itself. Please help!
        > >
        > > <start email>
        > > First, Casts from void * are necessary both in C and C++. This is from[/color][/color]
        the[color=blue]
        >
        > Casts from void* are necessary in C++, not in C.[/color]

        I am aware... I'm fishing for more info! Perhaps if I reposted again and
        said you were wrong I'll get more...

        You're WRONG!
        [color=blue]
        >[color=green]
        > > same reason you told me: otherwise, if the compiler wouldn't
        > > know what is the size of the object which is pointed at, it might get
        > > confused with arithmetic operations. You can ask him where did he[/color][/color]
        learned[color=blue][color=green]
        > > this rule from. tell me if you want me to correspond directly with him.
        > >
        > > Second, a possible reason to use integral types for addresses rather[/color][/color]
        than[color=blue][color=green]
        > > void *, is that in order to perform bitwise operations on an
        > > address, you have to use operators of an integral type.
        > > <end email>
        > >[/color]
        >
        > Regis
        >
        >[/color]

        Comment

        • Thomas Stegen

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

          Chris Fogelklou wrote:
          [color=blue]
          > Even better, here is the email itself. Please help!
          >
          > <start email>
          > First, Casts from void * are necessary both in C and C++. This is from the
          > same reason you told me: otherwise, if the compiler wouldn't
          > know what is the size of the object which is pointed at, it might get
          > confused with arithmetic operations. You can ask him where did he learned
          > this rule from. tell me if you want me to correspond directly with him.[/color]

          Yes, please tell her to come here. On the other hand, don't, tell
          her to read the archives. This conversation is getting old ;)

          the ISO/IEC 9899:1999 standard section 6.3.2.3 has what you want. That
          is C99 btw. Don't know the clause in C89.
          [color=blue]
          >
          > Second, a possible reason to use integral types for addresses rather than
          > void *, is that in order to perform bitwise operations on an
          > address, you have to use operators of an integral type.[/color]

          This much is true. But it is in implementation defined behaviour land,
          most compilers behave very nicely when it comes to this though.

          --
          Thomas.


          Comment

          • Thomas Stegen

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

            Chris Fogelklou wrote:
            [color=blue]
            > What is wrong with the above?
            >
            > Don't worry, I already know (learned my lesson last week.) It is for the
            > benefit of our resident compiler guru who seems to think you need the cast.
            > I thought it too, up until I started posting here!
            >[/color]

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

            There is nothing wrong with the above. It is considered a better
            idiom to use

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

            The argument that the compiler does not know the type of the object
            is bogus since all the information is already there. The compiler knows
            the type of p, it knows the type of the value returned by malloc,
            nothing is missing.

            In my previous reply I mentioned 6.3.2.3, I should also have mentioned
            clause 6.5.4 and 6.5.16.1 which is where the "no cast required" part is.

            --
            Thomas.

            Comment

            • Régis Troadec

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


              "Chris Fogelklou" <chris.fogelklo u@comhem.se> a écrit dans le message de
              news:3Hxfc.8976 2$dP1.269154@ne wsc.telia.net.. .

              Yes, sorry for that, i read your post too quickly,
              [color=blue]
              > "Régis Troadec" <regt@wanadoo.f r> wrote in message
              > news:c5m8hk$hc5 $1@news-reader3.wanadoo .fr...[color=green]
              > >
              > > "Chris Fogelklou" <chris.fogelklo u@comhem.se> a écrit dans le message de
              > > news:Snxfc.8975 9$dP1.269223@ne wsc.telia.net.. .[color=darkred]
              > > > "Chris Fogelklou" <chris.fogelklo u@comhem.se> wrote in message[/color]
              > >
              > > Hi,
              > >[color=darkred]
              > > > news:7lxfc.8975 8$dP1.269223@ne wsc.telia.net.. .
              > > > > What is wrong with the above?
              > > > >
              > > > > Don't worry, I already know (learned my lesson last week.) It is[/color][/color][/color]
              for[color=blue][color=green]
              > > the[color=darkred]
              > > > > benefit of our resident compiler guru who seems to think you need[/color][/color][/color]
              the[color=blue][color=green][color=darkred]
              > > > cast.
              > > > > I thought it too, up until I started posting here!
              > > > >
              > > > > Thanks,
              > > > >
              > > > > Chris
              > > > >
              > > > Even better, here is the email itself. Please help!
              > > >
              > > > <start email>
              > > > First, Casts from void * are necessary both in C and C++. This is from[/color][/color]
              > the[color=green]
              > >
              > > Casts from void* are necessary in C++, not in C.[/color]
              >
              > I am aware... I'm fishing for more info! Perhaps if I reposted again and
              > said you were wrong I'll get more...
              >
              > You're WRONG![/color]

              void* is implicitly converted to the destination pointer type (C99 §6.3.2.3
              and §7.20.3.1).
              [color=blue][color=green]
              > >[color=darkred]
              > > > same reason you told me: otherwise, if the compiler wouldn't
              > > > know what is the size of the object which is pointed at, it might get
              > > > confused with arithmetic operations.[/color][/color][/color]

              sizeof ?
              [color=blue]
              >You can ask him where did he
              > learned[color=green][color=darkred]
              > > > this rule from. tell me if you want me to correspond directly with[/color][/color][/color]
              him.[color=blue][color=green][color=darkred]
              > > >
              > > > Second, a possible reason to use integral types for addresses rather[/color][/color]
              > than[color=green][color=darkred]
              > > > void *, is that in order to perform bitwise operations on an
              > > > address, you have to use operators of an integral type.[/color][/color][/color]

              It's also said in the standard (C99), part 6 of §6.3.2.3 that :
              "Any pointer type may be converted to an integer type. Except as previously
              specified, the result is implementation-defined. If the result cannot be
              represented in the integer type, the behavior is undefined. The result need
              not be in the range of values of any integer type."

              Is he/she sure that he/she can always hold his adressses with an integral
              type ? It implies sizeof(the famous integral type) >= sizeof(void*).

              There is a way in C99 to hold pointers and adresses with integers thanks to
              the types intptr_t and uintptr_t. (C99 7.18.1.4), but I'm not sure it's
              possible in C90..

              Regis
              [color=blue][color=green][color=darkred]
              > > > <end email>
              > > >[/color]
              > >
              > > Regis
              > >
              > >[/color]
              >[/color]


              Comment

              • Chris Fogelklou

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

                "Thomas Stegen" <tstegen@cis.st rath.ac.uk> wrote in message
                news:c5m9qn$3c0 d0$1@ID-228872.news.uni-berlin.de...[color=blue]
                > Chris Fogelklou wrote:
                >[color=green]
                > > What is wrong with the above?
                > >
                > > Don't worry, I already know (learned my lesson last week.) It is for[/color][/color]
                the[color=blue][color=green]
                > > benefit of our resident compiler guru who seems to think you need the[/color][/color]
                cast.[color=blue][color=green]
                > > I thought it too, up until I started posting here!
                > >[/color]
                >
                > struct my_struct *p = (struct my_struct *)malloc(sizeof (struct
                > my_struct));
                >
                > There is nothing wrong with the above. It is considered a better
                > idiom to use
                >
                > struct my_struct *p = malloc(sizeof *p);
                >
                > The argument that the compiler does not know the type of the object
                > is bogus since all the information is already there. The compiler knows
                > the type of p, it knows the type of the value returned by malloc,
                > nothing is missing.
                >
                > In my previous reply I mentioned 6.3.2.3, I should also have mentioned
                > clause 6.5.4 and 6.5.16.1 which is where the "no cast required" part is.
                >
                > --
                > Thomas.
                >[/color]

                Hi Thomas,

                Thanks! OK, then what about this:

                int my_func(void *pinst)
                {
                my_struct *pthis = (my_struct *)pinst; //Cast
                pthis->print("Lets see what's wrong with this!");
                return 0;
                }

                If it is OK to never cast to/from void * as was made abundantly clear last
                week, then this is also OK. However, pinst could be a run-time defineable
                value.

                Thanks!

                Chris

                Comment

                • CBFalconer

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

                  Chris Fogelklou wrote:[color=blue]
                  > "Chris Fogelklou" <chris.fogelklo u@comhem.se> wrote in message
                  >[color=green]
                  > > What is wrong with the above?
                  > >
                  > > Don't worry, I already know (learned my lesson last week.) It
                  > > is for the benefit of our resident compiler guru who seems to
                  > > think you need the cast. I thought it too, up until I started
                  > > posting here!
                  > >[/color]
                  > Even better, here is the email itself. Please help!
                  >
                  > <start email>
                  > First, Casts from void * are necessary both in C and C++. This is
                  > from the same reason you told me: otherwise, if the compiler
                  > wouldn't know what is the size of the object which is pointed at,
                  > it might get confused with arithmetic operations. You can ask him
                  > where did he learned this rule from. tell me if you want me to
                  > correspond directly with him.
                  >
                  > Second, a possible reason to use integral types for addresses
                  > rather than void *, is that in order to perform bitwise operations
                  > on an address, you have to use operators of an integral type.
                  > <end email>[/color]

                  Don't get too antsy with him/her. This sounds like some system
                  programming operations on some embedded system, where the rules
                  are often different. The cast of the malloc remains unnecessary
                  (and don't put the question in the subject only).

                  Do learn to organize your questions into a single consistent
                  message, rather than a bit here and a bit there.

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

                  • Chris Fogelklou

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

                    "Régis Troadec" <regt@wanadoo.f r> wrote in message
                    news:c5maek$b90 $1@news-reader4.wanadoo .fr...[color=blue]
                    >
                    > "Chris Fogelklou" <chris.fogelklo u@comhem.se> a écrit dans le message de
                    > news:3Hxfc.8976 2$dP1.269154@ne wsc.telia.net.. .
                    >
                    > Yes, sorry for that, i read your post too quickly,
                    >[color=green]
                    > > "Régis Troadec" <regt@wanadoo.f r> wrote in message
                    > > news:c5m8hk$hc5 $1@news-reader3.wanadoo .fr...[color=darkred]
                    > > >
                    > > > "Chris Fogelklou" <chris.fogelklo u@comhem.se> a écrit dans le message[/color][/color][/color]
                    de[color=blue][color=green][color=darkred]
                    > > > news:Snxfc.8975 9$dP1.269223@ne wsc.telia.net.. .
                    > > > > "Chris Fogelklou" <chris.fogelklo u@comhem.se> wrote in message
                    > > >
                    > > > Hi,
                    > > >
                    > > > > news:7lxfc.8975 8$dP1.269223@ne wsc.telia.net.. .
                    > > > > > What is wrong with the above?
                    > > > > >
                    > > > > > Don't worry, I already know (learned my lesson last week.) It is[/color][/color]
                    > for[color=green][color=darkred]
                    > > > the
                    > > > > > benefit of our resident compiler guru who seems to think you need[/color][/color]
                    > the[color=green][color=darkred]
                    > > > > cast.
                    > > > > > I thought it too, up until I started posting here!
                    > > > > >
                    > > > > > Thanks,
                    > > > > >
                    > > > > > Chris
                    > > > > >
                    > > > > Even better, here is the email itself. Please help!
                    > > > >
                    > > > > <start email>
                    > > > > First, Casts from void * are necessary both in C and C++. This is[/color][/color][/color]
                    from[color=blue][color=green]
                    > > the[color=darkred]
                    > > >
                    > > > Casts from void* are necessary in C++, not in C.[/color]
                    > >
                    > > I am aware... I'm fishing for more info! Perhaps if I reposted again[/color][/color]
                    and[color=blue][color=green]
                    > > said you were wrong I'll get more...
                    > >
                    > > You're WRONG![/color]
                    >
                    > void* is implicitly converted to the destination pointer type (C99[/color]
                    §6.3.2.3[color=blue]
                    > and §7.20.3.1).
                    >[color=green][color=darkred]
                    > > >
                    > > > > same reason you told me: otherwise, if the compiler wouldn't
                    > > > > know what is the size of the object which is pointed at, it might[/color][/color][/color]
                    get[color=blue][color=green][color=darkred]
                    > > > > confused with arithmetic operations.[/color][/color]
                    >
                    > sizeof ?
                    >[color=green]
                    > >You can ask him where did he
                    > > learned[color=darkred]
                    > > > > this rule from. tell me if you want me to correspond directly with[/color][/color]
                    > him.[color=green][color=darkred]
                    > > > >
                    > > > > Second, a possible reason to use integral types for addresses rather[/color]
                    > > than[color=darkred]
                    > > > > void *, is that in order to perform bitwise operations on an
                    > > > > address, you have to use operators of an integral type.[/color][/color]
                    >
                    > It's also said in the standard (C99), part 6 of §6.3.2.3 that :
                    > "Any pointer type may be converted to an integer type. Except as[/color]
                    previously[color=blue]
                    > specified, the result is implementation-defined. If the result cannot be
                    > represented in the integer type, the behavior is undefined. The result[/color]
                    need[color=blue]
                    > not be in the range of values of any integer type."
                    >
                    > Is he/she sure that he/she can always hold his adressses with an integral
                    > type ? It implies sizeof(the famous integral type) >= sizeof(void*).
                    >
                    > There is a way in C99 to hold pointers and adresses with integers thanks[/color]
                    to[color=blue]
                    > the types intptr_t and uintptr_t. (C99 7.18.1.4), but I'm not sure it's
                    > possible in C90..
                    >
                    > Regis
                    >[color=green][color=darkred]
                    > > > > <end email>
                    > > > >
                    > > >
                    > > > Regis
                    > > >
                    > > >[/color]
                    > >[/color]
                    >
                    >[/color]
                    Do you know if this was also a standard in C89/C90? If so, it would
                    strengthen my argument since the recent response was that "GCC-derived
                    compilers don't fully support C99 yet." Not sure if it's true... Any
                    thoughts?

                    Not sure what she meant with arithmetic operations... you don't normally do
                    arithmetic on pointers, but if you did, I guess you could just cast to an
                    integral type of the correct size, correct?

                    The fact is I am very uncomfortable holding addresses in an integral data
                    type. Usually we let the compiler choose the addressing scheme (depending
                    on the memory model) and it should (if it is smart) know how big to make
                    pointers. But if all of our generic pointers are integral and need to be
                    casted to pointers, it's our own code that must know what the memory model
                    is.

                    Thanks in advance!

                    Chris

                    Comment

                    • Chris Torek

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

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

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

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

                            • Régis Troadec

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


                              "Dan Pop" <Dan.Pop@cern.c h> a écrit dans le message de
                              news:c5mf3u$b63 $1@sunnews.cern .ch...[color=blue]
                              > In <c5mdq4$msh$1@n ews-reader4.wanadoo .fr> "Régis Troadec"[/color]
                              <regt@wanadoo.f r> writes:[color=blue]
                              >[/color]

                              Hi,
                              [color=blue][color=green]
                              > >"Dan Pop" <Dan.Pop@cern.c h> a écrit dans le message de
                              > >news:c5mblu$1r p$3@sunnews.cer n.ch...[color=darkred]
                              > >> In <c5maek$b90$1@n ews-reader4.wanadoo .fr> "Régis Troadec"[/color]
                              > ><regt@wanadoo. fr> writes:[color=darkred]
                              > >>
                              > >>
                              > >> >"Chris Fogelklou" <chris.fogelklo u@comhem.se> a écrit dans le message[/color][/color][/color]
                              de[color=blue][color=green][color=darkred]
                              > >> >news:3Hxfc.897 62$dP1.269154@n ewsc.telia.net. ..
                              > >> >
                              > >> >> You're WRONG!
                              > >> >
                              > >> >void* is implicitly converted to the destination pointer type (C99[/color]
                              > >§6.3.2.3[color=darkred]
                              > >> >and §7.20.3.1).
                              > >>
                              > >> You're WRONG!
                              > >>
                              > >> There is NOTHING in C99 §6.3.2.3 or §7.20.3.1 supporting your[/color][/color][/color]
                              assertion.[color=blue][color=green]
                              > >
                              > >Let's correct it : void * may be converted to a pointer to any incomplete[/color][/color]
                              or[color=blue][color=green]
                              > >object type. As well, void* may be converted from a pointer to any
                              > >incomplete or object type. This is what I read in C99 §6.3.2.3.
                              > >You were probably hurt by seeing "void* IS ... converted".[/color]
                              >
                              > I was hurt by not seeing *anywhere* in 6.3.2.3 that the conversion
                              > doesn't require a cast. I am still hurt... ;-)[/color]

                              Perhaps one day I'll be right and exhaustive...
                              Let's now add C99 §6.5.4 concerning the cast operator and the further
                              reference to simple assignments in §6.5.16.1.
                              I'm too lazy to find out the interesting and relevant parts in the
                              paragraphs but I think the reader will find something that prove the
                              conversion doesn't require a cast.
                              Am I complete now, or is something missing ?

                              Regards,

                              Regis
                              [color=blue]
                              >
                              > Dan
                              > --
                              > Dan Pop
                              > DESY Zeuthen, RZ group
                              > Email: Dan.Pop@ifh.de[/color]


                              Comment

                              Working...