Undefined behavior when passing C++ objects to C routine -


update

I've found that if I pass in the context by the manufacturer I do this, it fixes the problem in A.cpp!

i.e. get information (vector & lt; double & gt; and amp0; vector & lt; double & gt; & amp; arg1 ...) , but what is the reason ?

Update

Actually I want to call some code from c ++.

As explained in the C manual, to avoid using the global variable, "Futile * FDATA" is provided to receive additional information, if none, then tap it Pointing at

  int f (unsigned nandy, unsigned npts, const double * x, zero * fdata, unsigned fdim, double * fval);   

Now I have to go through the "F" packing some C + objects and through this FDAATA logic, the way I could imagine that it was a class "Infops" Define, and pass it to C. Caratine

My C ++ snippet (e.g. A CPP and BQP, A does not work properly, while B is fine):

  // example eg .cpp #include "Cubature.h" // c library which includes cubature #include "extern_cpp_class.hpp" // class InfoPass {public: extern_cpp_class obj1; Extern_cpp_class obj2; Extern_cpp_class obj3; Double ARR [3]; Information about obj1 {arg0, arg1}, obj2 {arg0, arg2}, obj3 {arg0, arg3} {}} (vector & lt; double & gt; arg 0, vector & lt; double & gt; arg1, vector & Lt; double & gt; arg2, vector & lt; double & gt; arg3); // The declaration of int f () and coupure () below is in c code int f (unsigned ndim, const double * x, zero * fdata, unsigned fdim, double * fval); Int main () {doing something} / information passes caps {arg0, arg1, arg2, arg3}; // Early cube (2, f, and cubaspass, 2, xmin, xmx, 1e5, 0, 1a -5, errorOPARED, output, and iglia); / *** with process output *** int} (unsigned nandi, const double * x, void * fdata, unsigned fdim, double * fval) {InfoPass * fcubpass = static_cast & lt; InfoPass * & gt; (Fdata); / *** works with fcubpass.obj1, .obj2 ... *** /}   

Now, I can compile (GCC) and run Example A For, weird, there are undefinded behaviors, sometimes it gives NaN, sometimes it gives very crazy numbers ...

However, if I do this in the following way (eg B, Use points for the category) then use "new" in f, works fine! wondering why? Since I like the example A to B, in which I need "new" object ... BCPP class information page {public: extern_cpp_class * obj1 = NULL; Extern_cpp_class * obj2 = NULL; Extern_cpp_class * obj3 = NULL; Double ARR [3]; ~ InfoPass () {obj1; Remove obj2; Delete Obj3; }} Int main () {do something / InfoPass cubpass; // declared cubpass.obj1 = new extern_cpp_class (arg0, arg1); Cubpass.obj2 = new extern_cpp_class (arg0, arg2); Cubpass.obj3 = new extern_cpp_class (arg0, arg3); Cubicures (2, F, and Cubaspass, 2, Axmin, Xmx, 1E5, 0, 1A-5, Arrowpear, OUTTPUT, and Eggli); / *** with process output *** int} (unsigned nandi, const double * x, void * fdata, unsigned fdim, double * fval) {InfoPass * fcubpass = static_cast & lt; InfoPass * & gt; (Fdata); / *** fcubpass-> obj1, .obj2 ... *** /}

< P> Only one shot here in the dark

What does extern_cpp_class do the items with their initial parameters? If they take their vector logic in the references and store it, then you get annoyed with the original A.cpp because logic is temporary copies that have been destroyed. Disallowing references - After the cubpass the execution of the manufacturer has expired. On references to change, it must be ensured that the extern_cpp_class object is vector s has been created in the main , which (Possibly) Unless you are out of the program (or at least as long as you are working with cubpass ) in B.cpp Constructors already receive references to such vector s, so there is no problem.

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

c++ - Qt::make_shared for creating QSharedPtr as std::make_shared for creating std::shared_ptr -