Insecure operations on C pointers

Source: Internet
Author: User

Yesterday, I just consulted Mr. Liu and had a deeper understanding of the insecure operations of C language pointers.

Pointer beginners often take char * P = "Hello World" as a matter of course, and are overwhelmed by the "this memory cannot be written" error.

In fact, it is a pointer pointing to the starting address of the String constant. The content of this address is read-only and cannot be written.

Pointer:

First, we need to know that char * P declares a pointer, but we do not require it to point to which memory area, so it randomly points to a certain place. This part may be either a blank memory area or a region where data is already stored. If it is the former, it does not matter. If it is the latter, it will overwrite the data so thatProgramDamaged, causing a crash. This is the so-called insecure pointer application.

So how can we use pointers safely?

In fact, it is very easy to remember, "always let the pointer point to the place you want", instead of letting it randomly point to a place. This prevents the destruction of useful data and eliminates unsafe factors. For example:

Char string [] = "Hello World ";

Char * P;

P = string;

Okay, so that * P points to the header of the string [] we opened, everything is safe (unless you increase the increment when adding the pointer address to the end of the memory occupied by the string or to the front of the memory ).

If you do not learn pointers well, you will not be able to learn the C language well. In advanced languages, only the C language can be so flexible and powerful to directly control the hardware, which is the first language for MCU control. This is the future of C learning.

Be good at C, be a driver, and be an OS

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.