Skip to main content

Criticism of C++

C++ is a general-purpose programming language with imperative,object-oriented and generic programming features. Many criticisms have been leveled at the programming language from, among others, prominent software developers like Linus Torvalds,[1] Richard Stallman,[2] and Ken Thompson.[3]

C++ is a multiparadigm programming language[4] with backward compatibilitywith the programming language C.[5] This article focuses not on C features likepointer arithmeticoperator precedence or preprocessor macros, but on pure C++ features that are often criticized.

Slow compile timesEdit

The natural interface between source files in C/C++ are header files. Each time a header file is modified, all source files that include the header file should recompile their code. Header files are slow because of them being textual and context dependent as a consequence of the preprocessor.[6] C only has limited amounts of information in header files, the most important being struct declarations and function prototypes. C++ stores its classes in header files and they are not only exposing their public variables and public functions (like C with its structs and function prototypes) but also their private functions. This forces unnecessary recompiles of all source files that include the header file, each time when changing these private functions. This problem is magnified where the classes are written as templates, forcing all of their code into the slow header files, which is the case with the whole C++ standard library. Large C++ projects can therefore be extremely slow to compile.[7]
One solution for this is to use the Pimpl idiom. By using pointers on the stack to the implementation object on the heap there is a higher chance all object sizes on the stack become equal. This of course comes with the cost of an unnecessary heap allocation for each object. Additionally precompiled headerscan be used for header files that are fairly static.
One suggested solution is to use a module system.[8]

Global format state of <iostream>Edit

C++ <iostream> unlike C <stdio.h> relies on a global format state. This fits very poorly together with exceptions, when a function must interrupt the control flow, after an error, but before resetting the global format state. One fix for this is to use Resource Acquisition Is Initialization (RAII) which is implemented inBoost[9] but is not a part of the C++ Standard Library.
The global state of <iostream> uses static constructors which causes overhead.[10] Another source of bad performance is the use of std::endl instead of '\n' when doing output, because of it calling flush as a side effect. C++ <iostream> is by default synchronized with <stdio.h> which can cause performance problems. Shutting it off can improve performance but forces giving up thread safety.
Here follows an example where an exception interrupts the function before std::cout can be restored from hexadecimal to decimal. The error number in the catch statement will be written out in hexadecimal which probably isn't what one wants:
#include <iostream>
#include <vector>

int main() {
    try {
        std::cout << std::hex;
        std::cout << 0xFFFFFFFF << std::endl;
        std::vector<int> vector(0xFFFFFFFFFFFFFFFFL,0); // Exception
        std::cout << std::dec; // Never reached
    } catch(std::exception &e) {
        std::cout << "Error number: " << 10 << std::endl; // Not in decimal
    }
    return(EXIT_SUCCESS);
}
It is acknowledged even by some members of the C++ standards body[11] that the iostreams interface is an aging interface that needs to be replaced eventually. This design forces the library implementers to adopt solutions that impact performance greatly.[citation needed]

Heap allocations in containersEdit

After the inclusion of the STL in C++, its templated containers were promoted while the traditional C arrays were strongly discouraged.[12] One important feature of containers like std::string and std::vector is them having their memory on the heap instead of on the stack like C arrays.[13][14] To stop them from allocating on the heap, one would be forced to write a custom allocator, which isn't standard. Heap allocation is slower than stack allocation which makes claims about the classical C++ containers being "just as fast" as C arrays somewhat untrue.[15][16][not in citation given][improper synthesis?] They are just as fast to use, but not to construct. One way to solve this problem was to introduce stack allocated containers like boost::array[17] or std::array.
As for strings there is the possibility to use SSO (short string optimization) where only strings exceeding a certain size are allocated on the heap. There is however no standard way in C++ for the user to decide this SSO limit and it remains hard coded and implementation specific.[18][additional citation needed]

IteratorsEdit

The philosophy of the Standard Template Library (STL) embedded in the C++ Standard Library is to use generic algorithms in the form of templates usingiterators. Iterators are hard to implement efficiently which caused Alexander Stepanov to blame some compiler writers for their initial weak performance.[19]The complex categories of iterators have also been criticized,[20][21] and ranges have been proposed for the C++ standard library.[22]
One big problem is that iterators often deal with heap allocated data in the C++ containers and becomes invalid if the data is independently moved by the containers. Functions that change the size of the container often invalidate all iterators pointing to it, creating dangerous cases of undefined behavior.[23][24]Here is an example where the iterators in the for loop get invalidated because of the std::string container changing its size on the heap:
#include <iostream>
#include <string>

int main() {
    std::string text = "One\nTwo\nThree\nFour\n";
    // Let's add an '!' where we find newlines
    for(auto i = text.begin(); i != text.end(); ++i) {
        if(*i == '\n') {
            // i = 
            text.insert(i,'!')+1; 
            // Without updating the iterator this program has 
            // undefined behavior and will likely crash
        }
    }
            
    std::cout << text;
    return(EXIT_SUCCESS);
}

Uniform initialization syntaxEdit

The C++11 uniform initialization syntax and std::initializer_list share the same syntax which are triggered differently depending on the internal workings of the classes. If there is a std::initializer_list constructor then this is called. Otherwise the normal constructors are called with the uniform initialization syntax. This can be confusing for beginners and experts alike[25][26]
#include <iostream>
#include <vector>

int main() {
    int integer1{10}; // int
    int integer2(10); // int
    std::vector<int> vector1{10,0}; // std::initializer_list
    std::vector<int> vector2(10,0); // size_t,int
    
    std::cout << "Will print 10" 
    << std::endl << integer1 << std::endl;
    std::cout << "Will print 10" 
    << std::endl << integer2 << std::endl;
    
    std::cout << "Will print 10,0," << std::endl;
    for(auto &i : vector1) std::cout << i << ',';
    std::cout << std::endl;
    std::cout << "Will print 0,0,0,0,0,0,0,0,0,0," << std::endl;
    for(auto &i : vector2) std::cout << i << ',';

    return(EXIT_SUCCESS);
}

ExceptionsEdit

There have been concerns that the zero-overhead principle[27] isn't compatible with exceptions.[28] Most modern implementation has a zero performance overhead when exceptions are enabled but not used, but instead has an overhead in exception handling and in binary size due to the need for unroll tables. Many compilers support disabling exceptions from the language to save the binary overhead. Exceptions have also been criticized for being unsafe for state-handling, this safety issue has lead[dubious ] to the invention of the RAIIidiom, which has proven useful beyond making C++ exceptions safe.

Strings without UnicodeEdit

The C++ Standard Library offers no real support for Unicode. std::basic_string::length will only return the underlying array length which is acceptable when using ASCII or UTF-32 but not when using variable lengthencodings like UTF-8 or UTF-16. In these encodings the array length has little to do with the string length in code points. There are no support for advanced Unicode concepts like normalizationsurrogate pairsbidi or conversion between encodings.
This will print out the length of two strings with the equal amount of Unicodecode points:
#include <iostream>
#include <string>
#include <cassert>

int main() {
    // This will print "22 18", 
    // UTF-8 prefix just to be explicit
    std::string utf8  = u8"Vår gård på Öland!";
    std::string ascii = u8"Var gard pa Oland!";
    std::cout << utf8.length() << " " << ascii.length() << std::endl;
    assert(utf8.length() == ascii.length()); // Fail!
    return(EXIT_SUCCESS);
}

Verbose assembly and code bloatEdit

There has for a long time been accusations about C++ generating code bloat.[29][30]
source wikipedia

Comments

Popular posts from this blog

QBasic and its history

QBasic Not to be confused with  Quick Basic . QBasic Paradigm Procedural Developer Microsoft First appeared 1991 ; 25 years ago OS MS-DOS ,  Windows 95 ,  Windows 98 ,  Windows Me ,  PC DOS ,  OS/2 , eComStation License Part of the operating system (a variety of  closed-source  licenses) Website www .microsoft .com Influenced by QuickBASIC ,  GW-BASIC Influenced QB64 ,  Small Basic QBasic  ( Microsoft  Quick Beginners All purpose Symbolic Instruction Code ) is an  IDE  and  interpreter  for a variety of the  BASIC programming language  which is based on  QuickBASIC . Code entered into the IDE is compiled to an intermediate representation , and this  IR  is immediately interpreted on demand within the IDE. [1]  It can run under nearly all versions of  DOS  and  Windows , or through  DOSBox / DOSEMU , on  Linux  and...

Develop your website

Are you an enterpreneur and want a website to promote your buisness? If yes please choose your catogery so that you can feel easy. I need a attractive website i can invest required ammount I need a simple website and invest less Attractive website I can develop a website that explores everything of your buisness or profession you just need to bive me the detailed information of your buisness and i will evaluate it and decleare price if you agree to pay the work is started and will be completed within a month to 6 month as per expansivity of site  Simple Website I can also develop a simple website which will be informative source of your buisness and some speical features will be mentioned in site Note i will also give you a cms comment if you are intrested

BIOS

BIOS From Wikipedia, the free encyclopedia This article is about the BIOS as found in IBM PC compatibles. For similar programs on non-PC systems, see  booting . The  BIOS  ( / ˈ b aɪ . ɒ s / , an acronym for  Basic Input/Output System  and also known as the  System BIOS ,  ROM BIOS  or  PC BIOS ) is a type of  firmware  used to perform hardware initialization during the  booting  process (power-on startup) on  IBM PC compatible  computers, and to provide runtime services for operating systems and programs. [1]  The BIOS firmware is built into  personal computers  (PCs), and it is the first software they run when powered on. The name itself originates from the Basic Input/Output System used in the CP/M  operating system in 1975. [2] [3]  Originally  proprietary  to the IBM PC, the BIOS has been  reverse engineered  by companies looking to create compatible ...