简体   繁体   English

C ++中包含的C标准库函数是否抛出异常?

[英]Do C standard library functions which are included in C++ throw exception?

In the below code, author points that new operator function call might cause an exception so that this implementation is not exception safe because object state is already changed in the first line. 在下面的代码中,作者指出, new operator调用可能会导致异常,因此该实现不是异常安全的,因为第一行中的对象状态已更改。

String &String::operator =( const char *str ) {
    // state is changed
    delete [] s_;                                        

    if( !str ) str = "";

    // exception might occur because of new operator
    s_ = strcpy( new char[ strlen(str)+1 ], str );

    return *this;
}

While reading, I wondered that do C library functions throw exception in C++ ? 在阅读时,我想知道C库函数是否会在C ++中引发异常? I know that there is no exception in C but since we are using C++ compiler, there might be exceptions. 我知道C中没有例外,但是由于我们使用的是C ++编译器,因此可能会有例外。

So, can we consider c standard lib functions as exception safe function calls ? 因此,我们可以将c标准库函数视为异常安全函数调用吗?

Thank you. 谢谢。

Btw, for the record, right way (exception safe) to implement above function is below. 顺便说一句,下面记录实现上述功能的正确方法(异常安全)。

String &String::operator =( const char *str ) {
    if( !str ) str = "";
    char *tmp = strcpy( new char[ strlen(str)+1 ], str );
    delete [] s_;
    s_ = tmp;
    return *this;
}

While reading, I wondered that do C library functions throw exception in C++ ? 在阅读时,我想知道C库函数是否会在C ++中引发异常?

Not if you use the functions correctly. 如果正确使用这些功能,则不会。 How could they? 他们怎么会 That would completely break backward compatibility. 那将完全破坏向后兼容性。

However, if you use them incorrectly, undefined behaviour occurs. 但是,如果使用不正确,则会发生未定义的行为 And in that case, a compiler can do anything it wants, including the throwing of exceptions. 在这种情况下,编译器可以执行任何所需的操作,包括引发异常。

For example, take the following program which exhibits undefined behaviour according to the C++ language standard: 例如,采用以下程序,该程序根据C ++语言标准表现出未定义的行为:

#include <iostream>
#include <string.h>

int main()
{
    try
    {
        strcpy(nullptr, nullptr);
    }
    catch (...)
    {
        std::cerr << "exception\n";
    }
}

Compile it with Visual C++ 2013 using Structured Exception Handling as follows: 使用Visual C ++ 2013使用结构化异常处理对其进行编译,如下所示:

cl /nologo /Za /EHa /W4 stackoverflow.cpp

Result of the program: 程序结果:

exception

Generally speaking, the C++ standard does not modify any of the functions from the C standard, except to decree the identifiers should refer to actual functions rather than macros. 一般来说,C ++标准不会修改C标准中的任何功能,只是要声明标识符应引用实际功能而不是宏。 Thus, functions from the C standard library cannot throw an exception in any circumstance there the C standard decrees what a function can do. 因此,在任何情况下,如果C标准降低了函数的功能范围,C标准库中的函数都不会引发异常。

Loopholes that would allow exceptions include things like undefined behavior or callbacks (eg passing a comparator to qsort that throws an exception... although I'm not actually sure if that's allowed by the C++ standard). 允许异常的漏洞包括未定义的行为或回调(例如,将比较器传递给qsort引发异常……,尽管我实际上不确定C ++标准是否允许该漏洞)。 I don't think it particularly likely that library providers would spend any effort trying to add a feature of throwing exceptions in situations where there is undefined behavior. 我认为图书馆提供者不太可能会花任何努力来尝试添加在行为未定义的情况下引发异常的功能。

No - just because you compile with a C++ compiler doesn't mean exceptions will get thrown. 不-仅仅因为您使用C ++编译器进行编译并不意味着会引发异常。 C does not support exceptions, so a C program cannot throw any sort of exception. C不支持异常,因此C程序无法引发任何类型的异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM