Re: Porting C to C++
In article <ee5c23e6-f6dd-4f39-9993-
40d9f0500bdb@g3g2000pra.googlegroups.com>, paul.delamusica@gmail.com
says...
Hi,
I am porting a piece of code from C to C++. The C code has a macro
like this:
#define MALLOC(p, b, s) {if ((b) > 0) { \
p= malloc(b); if (!(p)) { \
fprintf(stderr, "memory allocation error:
%s\n", s); \
exit(0);}} else p= NULL;}
the g++ compiler complains about "invalid conversion from void* to"
xxx* because of the lack of a cast in front of p.
Is there a way to fix this macro so that it can work in C++?
Not really -- the macro simply doesn't fit well with the way C++ works.
Probaby more importantly, most code that uses the macro probably fits
with C++ even more poorly.
A reasonable minimal port would involve doing a global search and change
the code to use new instead. A more thorough port would generally
involve ripping out most of the code that uses the macro, and replacing
it with containers, probably from the standard library, but (depending
on what kinds of things the code does) possibly some custom containers
as well. Knowing nothing about the code, it's essentialy impossible to
say anything very definite, but a lot of use of malloc in C is to
implement various types of dynamic containers (strings, matrices, etc.)
and C++ already has quite a few of those pre-built in the standard
library.
--
Later,
Jerry.
The universe is a figment of its own imagination.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]