473,406 Members | 2,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

extern "C" function throwing an exception

Hi everyone,

I have the following code and it doesn't give any compilation error,

class A
{
public : A()
{
}
void show()
{
}
};

extern "C"
{

void fun1(void) throw (char)
{
cout<<"fun11"<<endl;
throw('C');
}
}

int main()
{
fun1();
return(0);
}

Is it an undefined behavior for a c++ function throwing an exception
to be specified in extern "C" linkage?

Thanks in advance ! ! !
Jun 27 '08 #1
2 5700
* Rahul:
Hi everyone,

I have the following code and it doesn't give any compilation error,

class A
{
public : A()
{
}
void show()
{
}
};

extern "C"
{

void fun1(void) throw (char)
{
cout<<"fun11"<<endl;
throw('C');
}
}

int main()
{
fun1();
return(0);
}

Is it an undefined behavior for a c++ function throwing an exception
to be specified in extern "C" linkage?
No, I don't think so, but it's undefined behavior to let a C++ exception
propagate through C code.

And extern "C" isn't very meaningful except to have that function called by C code.

Hence in your real set-up, as opposed to what you've shown, you probably have
undefined behavior, in practice that cleanup isn't performed during stack
unwinding through the C code.
Cheers, & hth.,

- Alf
Jun 27 '08 #2
On 12 avr, 20:28, "Alf P. Steinbach" <al...@start.nowrote:
* Rahul:
Is it an undefined behavior for a c++ function throwing an
exception to be specified in extern "C" linkage?
No, I don't think so, but it's undefined behavior to let a C++
exception propagate through C code.
What else could the C++ standard say about it? The C++ standard
cannot specify behavior in another language, so anything
happening in that language is undefined behavior.

Like all undefined behavior, it can be defined by the
implementation, and all of the implementations I know do define
it so that the exception will propagate through the C code. In
practice, I think it's something you can more or less count on,
*IF* the C and the C++ compiler come from the same source.
And extern "C" isn't very meaningful except to have that
function called by C code.
Hence in your real set-up, as opposed to what you've shown,
you probably have undefined behavior, in practice that cleanup
isn't performed during stack unwinding through the C code.
That's a different issue. C doesn't have any defined "cleanup"
to be performed, but you're almost certainly right that the C
programmer didn't design his code to be exception safe. So even
if the behavior is defined, it's likely in most cases to be
defined as something which won't work.

There is one major exception, I think: a lot of API's are
defined in terms of C (and require `extern "C"' on callback
functions passed into them), but are designed to be used from
both C and C++. In such cases, the library programmer does know
that C++ (and exceptions) is a possibility, and may have taken
it into account; if the library documents that exceptions will
propagate in such cases, you can use them. (Any library
*should* document such things. I have a sneaking suspicion,
however, than many don't.)

Note too that often, when passing a pointer to a function to
such an API, the function will be called asynchronously, much
later. For obvious reasons, exceptions can't be used there.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Shea Martin | last post by:
I am trying to use a system call which takes a function ptr. My compiler won't compile the code if I give the system_call a ptr to a class member function, A::func(). To combat this, I created an...
4
by: Sergei | last post by:
I ran into this problem. I needed to create an entry for access to a library of functions that are "extern C", and I just can't find the right syntax, if it exists at all ( I am using MSVC...
1
by: Phlip | last post by:
Language Lawyers: Compare this: extern "C" int Maine() { ... bool runTests(); return !runTests(); }
22
by: Ian | last post by:
The title says it all. I can see the case where a function is to be called directly from C, the name mangling will stuff this up. But I can't see a reason why a template function can't be...
10
by: Mark A. Gibbs | last post by:
I have a question about mixing C and C++. In a C++ translation unit, I want to define a function with internal linkage and C calling convention. Here's a sample of what I want to do: //...
19
by: ccwork | last post by:
Hi all, I am reading "C: A Reference Manual" 4th ed and I get lost for the "extern". It says that global object without specifying the storage-class specifier will have "extern" as the default...
4
by: kk_oop | last post by:
Hi. I need to write a C++ callback function and register it with a C program. I've read that this can be done if the callback function is a static method. I've also read that I should use a...
4
by: mimi | last post by:
The programmer indicates to the compiler that a function is written in a different programming language using a linkage directives.It is intuitive that extern "SomeLanguage" is used to declare...
5
by: Medvedev | last post by:
What's that preprocessor do #ifdef __cplusplus extern "C" { #endif .. .. .. #ifdef __cplusplus } #endif
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.