C ++ meditation transcript

Source: Internet
Author: User
In other words, books cannot be read without being borrowed ~~ It seems a bit reasonable. I have never read much since I bought several books at home because I have no time. I still have two books under my seat to use as a cushion ~~ In fact, it is not that time is not enough, but that it is not used for good use ~~ It seems that we need to improve it later. my colleague just took a copy of the C ++ meditation book and took a look at it. I didn't expect it to be too busy. It took only one day to finish reading it in one breath. although I have read it, I still understand some core ideas.

The book is written by a foreigner. The specific name cannot be remembered, but the translation quality is not bad. I haven't found many language problems. It's also smooth to read. Well, I don't have to talk about it anymore. Let's turn it into the question below.

The development of computer language to the era of C has been more and more important. C language is widely used as an excellent computer language, although it provides flexible and powerful functions, it also brings a lot of trouble. The most typical is the memory allocation and recovery, this is a core issue in this book. almost all problems come from this. because in C LanguageProgramUsers need to manage the memory usage by themselves, and it is particularly important to allocate and release the memory at the time. The C language introduces the pointer concept, which can lead to a series of problems:

Because the pointer is an identifier pointing to a memory address, if the address it points to is no longer an object defined by the pointer itself, it will cause unknown consequences and cause program crash.

If the allocated memory is not released in time, the memory usage will continue to rise, so that the system will become a machine after a period of operation.

For a set, the semantic difference between value reference and address reference is troublesome for system framework designers or program developers. for example, if we add an object to a set and then change the attributes of this object, do the objects in the Set need to reflect the current state of this object? In some cases, we may need to change it at the same time, but in other scenarios, we may want this original object to be two different units from the object in the set, the one in the set is only a clone generated during addition. in the later period, their statuses can be changed independently without affecting each other.

If it is an address reference, when the original object fails, the reference address in the set will point to an invalid memory address, unless you explicitly update the Set object, however, it is sometimes difficult for us to maintain this relationship.

For value reference, if the object still contains sub-objects, how can this problem be solved?

In order to make such a suitable set, that is, our container, some new ideas have been proposed, such as object reference counting, handle technology, and so on.AlgorithmAnd the data structure is abstracted, so there is something like the STD Library (similar to the C # generic technology). Of course, here we also need to consider someCodeWhat kind of interfaces, error handling mechanisms, etc. For example, what kind of things should be hidden in the internal reality, and what things should be handled by the customer code.

Another important thing is dynamic memory address allocation, because the capacity of an array is relatively fixed. If you need to change its size, you need to re-allocate the memory, sometimes we need such a container whose capacity can change dynamically. in this case, the complexity of the long work of the memory needs to be hidden through a certain data structure.

After learning about the C ++ content, let's look back at C #. You will find that using C # is so comfortable, because there are many things that CLR has done for you, for example, problems such as memory space allocation, when to remove an object from the memory (it will perform automatic object reference counting, and when it is set to zero, it may be recycled by GC), etc, GC plays an important role in it. this reminds me of the scenario in the hacker Empire: for people in the system, you may not feel anything, or even the existence of a "system, but when you break through this limit, you will see another world.

Even though c ++ is not eliminated due to its complexity, "it is more like some sophisticated tools ", you can use it to make some precise structures or other tools. It can run stably and at high speed for a long time, and like C #, it allows people to think more about the implementation of functional logic, this frees programmers from complicated memory management work and avoids many mistakes. although it sacrifices some performance at the cost, it is acceptable in many cases. What's more, in today's era of high hardware computing speed.

"Complexity is inevitable, but these complexity should be hidden to a higher abstraction layer", which is of course the trend, and C # is born. Just like computers, for a single part, they are actually quite complex. Think about the computers that used to be so large as several rooms. Now they can be so small, the reason is that it hides its internal complexity well. As users, we don't need to care about its implementation, whether it's Samsung's hard disk or Xijie's hard disk, both flash and SCSI can meet our storage needs. in this way, we can build more advanced structures.

All in all, memory management is the root cause of pain for C/C ++ programmers.

Related Article

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.