Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gcc - what is attribute nothrow used for?

Tags:

c

exception

gcc

I was taking a look through some of the gcc attributes list and I spotted this one that caught my attention:

nothrow
The nothrow attribute is used to inform the compiler that a function cannot
throw an exception. For example, most functions in the standard C library can be
guaranteed not to throw an exception with the notable exceptions of qsort and
bsearch that take function pointer arguments. The nothrow attribute is not
implemented in GCC versions earlier than 3.3. 

How could a C function throw an exception? Could someone explain what this attribute is used for?

There seems to be a nothrow tag available but what I've found there seem to be related to C++ std::nothrow. Not sure whether that's related to my particular question.

like image 296
dragosht Avatar asked Jul 22 '14 08:07

dragosht


1 Answers

This has significance when calling C code from C++ code, it guarantees to the compiler for optimization purposes that the C code cannot throw exceptions. This is useful if either it is C code and cannot throw, or because it is C++ written specifically to never throw up an exception.

So ultimately if you are writing pure C you should never need this, however if you think someone may call your code from C++ as a library it can be handy.

like image 128
Vality Avatar answered Nov 03 '22 09:11

Vality