Read defective C ++ 2

Source: Internet
Author: User
Read defective C ++ 2

If you do not know the harms of using soldiers, you cannot know the benefits of using them-Sun Tzu's Art of War

3. Can C ++ exceptions be avoided without throwing exceptions?
New T () throws an exception, that is, an exception may be thrown everywhere. Since the memory is not enough and it is not expected to continue running, it does not seem to be a big problem. However, if you try to allocate a large block of memory,ProgramIt is important to capture exceptions. In particular, although new will throw an exception, but the stack overflow will not, and the program will crash directly.

In addition, to enable P = new OBJ () to release the memory allocated by new when an exception occurs during the construction, the compiler automatically generates a pair of try-catch constructor, even if we do not use exceptions. Therefore, most compilers today provide compilation options that disable C ++ exceptions to remove this additional overhead.

4. Why do we have to use Delete [] P to release dynamically allocated arrays?
In various C ++ documents, only delete [] is required. Otherwise, a crash may occur. The strange thing is that malloc () and free () are not difficult. Observe the overload functions of new and delete, and you will find that the new parameter contains the size of the memory block, but delete does not. Obviously, the memory distributor knows the size of the memory block to which a pointer points. This information is contained in the heap data structure. One difference between Delete and free () is that it also calls the object's destructor. The strange thing is that delete does not use the size of the memory block divided by size (* P) to obtain the number of array elements. Instead, it allocates more elements when allocating memory in new. In this way, delete and delete [] have different interpretations of the Data zone pointed to by the pointer, which will cause the program to crash. Of course, it depends on the location where the compiler puts this number. It may just call the destructor of the first element to generate some unreleased resources.
This is an easy and hard-to-find error, and sometimes it is not obvious. For example, tvec3_t * P = new vec3_t; Delete P; is it wrong? After the definition is checked, the typedef float vec3_t [3] can be found;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.