C language Curse of the unseen error, those years of learning pointers from the introduction to the Earth are memory problems

Source: Internet
Author: User

It's that kind of love study

The moment from the beginning to the ground

Ugly refusal

Objective

Memory errors in C programs are very harmful: they are very common and can lead to serious consequences, most of the time the memory problems are invisible and can not be touched. Compile the correct operation error, let the novice from the beginning to the ground, let the veteran also headache, spend a lot of time to find and repair. Most of the time, the most serious security problems are caused by simple memory errors, which can cause software crashes and a system crash. Memory-related programming is so important, and proper application in practice is so difficult that it dominates the object-oriented programming language, the functional programming language, the Advanced programming language, the declarative programming language, and all the other variables or theories of other programming languages. Therefore, for all these reasons, special attention needs to be paid to C's memory issues. Let's take a look at how to solve these problems, first of all, not what language.

The mini-series will take you through some good and memory-related coding practices to keep memory errors within control.

The small part starts to install the force repaying after 30 meters

Memory Error classification

All the practical problems that may exist:

The problem is very serious, the reason is very simple

Memory leak: A memory leak occurs when a resource is allocated, but it is never recycled.

Small part of the analysis to see below

Have you seen the problem? Unless the Fucntion2 is unusually responsive to free memory, the call to Fucntion1 will leak 100 bytes each time. When memory sticks are incrementally distributing several megabytes of memory, a single leak is negligible, but even such a small leak can weaken the application after several hours of continuous operation.

Small part of the analysis to see below

The semantics of fopen require complementary fclose. In the absence of fclose, the C standard cannot specify what happens, most likely a memory leak. Other resources (such as semaphores, network handles, database connections, and so on) are also worth considering. Especially for C-language file operations, the file is not closed, it is easy to cause the file read and write failure.

Memory error allocation: initialization of pointers

This is still very simple.

These errors are usually less severe, and it is not a problem to have a little bit of a pointer concept.

Dangling pointer: Wild pointer (pointer not pointing)

This situation is especially common in C-language linked list deletion operations

Array boundary violation

Array boundary violations are dangerous, which is the last major category of memory error management. If an array size thing is 100, more than 100, what happens? Answer: unpredictable, but it may be very different from good situation. In particular, C copies a string that is not suitable for 100 characters assigned to it. In any general implementation, the "over" character overwrites other data in memory. The layout of the in-memory data distribution is complex and difficult to reproduce, so no symptoms can be traced back to a specific error at the source code level. These errors usually result in a loss of $ millions of.

I have a public number, often share some of the C language/c++ technology related dry goods; If you like my share, you can use the search "C language learning Tribe" to focus on
Welcome to join thousands of people to exchange questions and answers skirt: 627+012+464

C language Curse of the unseen error, those years of learning pointers from the introduction to the Earth are memory problems

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.