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