[Rd] Memory allocation in C/C++ vs R?

Dominick Samperi djsamperi at gmail.com
Fri Apr 30 22:38:11 CEST 2010


> Indeed. As I said, using heap objects (explicit initialization) does solve that issue, but you have to be again wary of other libraries which may still use static initializers.

I just did a little test and found, in the case of gcc/g++, that a
main C main program linked with a C++ library
that has static intializers works just fine. The sequence of commands was:

g++ -fpic -c mylib.cpp
g++ -shared -o foo.so mylib.o
gcc mylibtest foo.so

It appears that the runtime system / shared library loading process
makes sure that the static initializers for
classes defined in mylib.so are run before the C program defined in
mylibtest.c is started.

Of course, we knew this already ("it works when it works"), but this
shows, at least in the case of gcc/g++,
what is going on. But as you have pointed out, the whole process is
OS-dependent and may not work
as smoothly on other platforms.

Dominnick



More information about the R-devel mailing list