Converting C++ to C

Robert Bonomi bonomi at mail.r-bonomi.com
Fri Feb 24 19:13:02 UTC 2012


"Julian H. Stacey" <jhs at berklix.com> wrote;
> perryh at pluto.rain.com wrote:
> > Reason:  I want to make what I think would be a fairly minor
> > change to a small (1100-line) C++ program, but I don't know C++
> > -- only C -- and I don't understand the program well enough
> > to mess with it.  I suspect I would be able to figure out an
> > equivalent C program.
> > 
> > In case it matters, I'm using FreeBSD 8.1.
>
> One of the lists recently (maybe 2/3 weeks ago) carried a thread
> listing many C compilers past & present.  It started by discussing
> Clang V. GCC I can't remember which list, I don't think it was
> questions@ maybe hackers@ or current at .

There _was_ a recent discussion on 'questions' -- I'm the 'guilty party' 
responsible for naming a lot of the 'historical' ones.

That aside, for the OP:

 C code generated from C++ will _not_ be very readable.

 Basically, -everything- in C++ would get turned into a function invocation
 in the generated C.  With the _name_ of each such function having an encoded
 representation of the type of each argument to that function (see 'function
 name mangling).  And the "simple" elementary data types tend to end up as 
 something like: "**struct foo {bar value; (*(**struct foo)baz())[];}".
 Some of the mayhem: _everything_ is 'double indirect' pointers, to support 
 run-time automatic garbage collection; 'methods' of acting on data elements
 are pointers to functions, embedded in the data-element structure, even
 basic 'four function calculator' arithmetic ops (they can be 'overlaid'
 to do differnt things on different data types  -- the '+' operator may
 mean 'concatenation' when applied to two strings, or '+=' maay mean 
 'append item to list, in the contest of 'list += item', even though both
 would *still* mean 'addition' when used with numeric items.)

 One would be far better off spending some time to learn the basics of C++
 syntax -- to be able to 'read' the existing code and understand what it's
 doing.  After that, if what you want to modiy -is- truely a 'minor' change,
 adding some 'C-tyee' code to implement it is probably not that bad.



More information about the freebsd-questions mailing list