I am working on a small application that was written in C++ and would like to use on my platform. Unfortunately, our cross-compile toolchain only (reliably) provides a C compiler. I looked at the application, and it is fairly simple and only uses C++-specific idioms in a few places, so I thought I'd just convert it to C code by hand.
I bumped across one line that I'm not sure how to handle. The code is using Termios to open a new port to talk to a TTY stream, and initializes the Termios struct using the new
keyword.
termios *settings = new termios();
As I understand it, the new
keyword, in addition to allocating the appropriate memory, calls the object's initializer. In C, after I allocate memory with malloc
, can I manually call the initializer? Do I need to?
I have a feeling that I'm misunderstanding something obvious / fundamental or that I'm looking at this all wrong. I'm not very accustomed to C++ code.
edit: I seem to have caused some confusion. The line of code above is creating a new termios struct as defined in termios.h
, part of the standard libraries on most implementations of C.
The line
allocates memory for a
termios
object and value-initializes it. Sincetermios
is a POD, the equivalent C would beor
and of course the equivalent of
delete settings
would befree(settings)
.