Question about mixing c++ exception handling & c code

A

abhijeet

Hi,

I was looking thru code in my organisation. same dll is sharingcode is
mixing c++ code & c code, which is being again called by some other c+
+ dll.

Only issue I am not too sure about exception handling in this case -

here is trimmed code snippet -

include.h
typedef void (func*)(void *,bool);
struct scriptparams
{
func callback;
void *obj;
};

test.cpp
class execption
{
public:
virtual void printerror(){//do something};
};
class foo
{
public:
foo(){};
void dosomething(bool arg)
{
cout>>"do something">>endl;
if(!arg) throw execption;
//if valid do something
};
};
extern "C" void dosomethingwrapper(void *obj_,bool arg)
{
foo *fooptr = reinterpret_cast<foo*>obj_;
fooptr->dosomething(arg);
};

test1.c
#include <include.h>
void
executescript(scriptparams* params)
{
bool ifValid = false;
//calculate something to get value of
params->callback(params->obj,ifValid);
}

test2.cpp
#include <include.h>
class scriptexecuter
{
public:
scriptexecuter(char* arg1,char* arg2)
{// get these args & do someprocessing};
void execute()
{
scriptparams params;
params->callback = dosomethingwrapper;
params->obj = this;
try{
executescript(params);
}
catch(execption &e)
{
e.printerror();
}
};
};

test.cpp & test1.c are being compiled in one dll lets call dll1 & othe
being compiled into another dll -> dll2.

I am not sure what to expect if foo::dosomething raises an exception.
Is code safe for exception being traversing through c code.
Can I assume object which is being caught in scriptexecuter::execute
to be properly constructed one.

Thank you for your advice.

Regards,
Abhijeet
 
P

Peter

Hi,

I was looking thru code in my organisation. same dll is sharingcode is
mixing c++ code & c code, which is being again called by some other c+
+ dll.


There is no need to translate any code with a c-compiler.
Ansi C code compiles with a C++-compiler.
My advice is to get rid of C code in favor or exception-safe-C++ code.
 
P

Peter

I am not sure what to expect if foo::dosomething raises an exception.
Is code safe for exception being traversing through c code.


As long as the C-code is being translated using a C++ compiler you're
fine.
 
I

Ian Collins

Peter said:
There is no need to translate any code with a c-compiler.
Ansi C code compiles with a C++-compiler.

No, it does not. There's plenty of legal C that isn't legal C++.
 
P

peter koch

There is no need to translate any code with a c-compiler.
Ansi C code compiles with a C++-compiler.
My advice is to get rid of C code in favor or exception-safe-C++ code.

While I agree with you that most valid C code very likely also either
is valid C++ code or trivially can be changed into legal C++ code, it
is not enough to also make it "real" C++ code. Most important in this
aspect is the lack of exception safety, and this alone should be
reason enough to refrain you from taking such a short-cut.

/Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,069
Latest member
SimplyleanKetoReviews

Latest Threads

Top