Manually invoke the C ++ object initializer in C
I am working on a small application written in C ++ and would like to use it on my platform. Unfortunately, our toolchain cross-compile (reliably) provides a C compiler. I looked at the appendix and it's pretty simple and only uses C ++ - specific idioms in a few places, so I thought I'd just recode it to C code by hand.
I came across one line and I'm not sure how to handle it. The code uses Termios to open a new port to communicate with the TTY stream and initializes the Termios structure with a keyword new
.
termios *settings = new termios();
As I understand it, the keyword new
, in addition to allocating the appropriate memory, calls the object's initializer. In C, after allocating memory with malloc
, is it possible to manually call the initializer? I need?
I have a feeling that I am missing something obvious / fundamental or that I am looking at the whole thing in the wrong way. I am not very used to C ++ code.
edit: I seem to have caused some confusion. The line of code above creates a new termios structure defined in termios.h
, part of the standard libraries in most C implementations.
source to share
Line
termios *settings = new termios();
allocates memory for the object termios
and value-initializes it. Since termios
is a POD, the equivalent of C would be
struct termios* settings = calloc(1, sizeof(*settings));
or
struct termios* settings = malloc(sizeof(*settings));
memset(settings, 0, sizeof(*settings));
and of course the equivalent delete settings
would be free(settings)
.
source to share
"In C, after allocating memory with malloc, can I manually call the initializer?"
Sorry, you can't.
If necessary? "
It really depends on the definition of the objects termios
. Basically what it malloc
does is just allocate a chunk of memory. That is, it does not perform any initialization and internal memory allocations such as the constructor.
What do I do in the following situations:
I am creating C wrapper functions for my C ++ objects using opaque pointers. For example, to call the constructor of a C ++ object, I would create a C ++ wrapper for C in a file .cpp
:
void* create_termios() { return new termios(); }
void destroy_termios(void *obj) { delete obj; }
// other wrapper functions for termios
And then I would link these C functions to a file .h
:
extern "C" {
void* create_termios();
void destroy_termios(void *obj);
// declare any other necessary wrappers
}
source to share
Note that termios
is the name struct
associated with termios (3) , so it is best not to use this termios
name in a Linux or POSIX C program (i.e. avoid naming your types with the normal types provided by the system libraries).
BTW you should consider using some existing terminal I / O library like ncurses or readline
Finally, if you insist that you have your own struct or class termios
(which is practically a very bad idea, choose a different name) managed by your C ++ library that is called by C, you should wrap its allocator + constructor and destructor + deallocator like this.
extern "C" struct termios* make_my_termios () {
struct termios* ts = new termios;
return ts;
}
extern "C" void destroy_my_termios(struct termios* ts) {
delete ts;
}
And if you are just using the genuine struct termios*
(from termios (3) ) in your C ++ library, just keep it as it is ...
source to share