Floating pointer and wild pointer level two pointer in C + +

Source: Internet
Author: User

(1) Hover pointer in C + +: A pointer that declares but does not have a value, pointing to any space in memory. One way to avoid suspending the pointer is to start with a value of NULL

(2) "Wild pointer" is not a null pointer, it is a pointer to "junk" memory. It is generally not wrong to use a null pointer because it is easy to judge with an if statement. But a "wild pointer" is dangerous, and the IF statement does not work on it. There are two main causes of wild pointers:

One, the pointer variable is not initialized. Any pointer variable that has just been created does not automatically become a null pointer, and its default value is random, and it can be arbitrary. Therefore, the pointer variable should be initialized at the same time it is created, either by setting the pointer to null or by pointing it to legitimate memory.
Two The pointer p is not set to null after the free or delete, which makes the person think P is a valid pointer. Don't look at the names of free and delete (especially the delete), They simply release the memory that the pointer refers to, but do not kill the pointer itself. Usually, the statement if (P! = NULL) is used for error-proof handling. Unfortunately, the IF statement is not error-proof at this point because even p is not a null pointer, it does not point to a valid block of memory. Cases:
Char *p = (char *) malloc (100);
strcpy (P, "hello");
Free (p); The memory referred to by P is freed, but the address referred to by P remains unchanged
if (P! = NULL)//does not play an anti-error role
strcpy (P, "World"); Error

third, another issue to note: Do not return a pointer or reference to the stack memory, because the stack memory will be released at the end of the function. strlen is to char*, string not, this is very easy to make people misunderstand ah

Iv. mistakes that we make easy:

For the second error is easy to appear in C + +, such as in the definition of the class constructor and destructor, if in the constructor of the dynamic Open (new), in the destructor to release, however, we generally delete the memory after the end of it, as everyone knows, Pointer to the previous memory is a wild pointer (stray pointer), alittle careless, will be wrong, when you assign value to the unknown area, good luck will be the program run error, if bad luck, it is likely to cause the system crash!
WORKAROUND: Make a pointer to an unknown region (a pointer that is just defined or free of memory) equal to null or point to a constant, and then use the pointer to determine NULL

whether or not you want to set to null after the delete occurs? The only criterion is the future will not use it, if it is possible to use, it must be set to NULL, otherwise, unless the performance requirements of the software is very strong, otherwise, although every time after the delete is set null good to do so, will never run error, the potential consequences is that, This null-value operation wastes less than one out of 10,000 seconds of your time.

V. Knowledge Supplement:

In general, the memory leaks that we often say refer to the leaks in heap memory . Heap memory means that the program is allocated from the heap, arbitrarily sized (the size of the memory block can be determined during the program's run time), and the freed memory must be displayed after use. Applications typically use functions such as malloc,realloc,new to allocate a piece of memory from the heap, and after use, the program must be responsible for calling free or delete to release the memory block, otherwise the memory will not be reused, we say this memory leaks

六、二级 pointer

Second-level pointer: pointer to pointer variable
For example: a point to B, B points to the direction of C, if a, B, C are variables, that is, C is a normal variable, B is a first-class pointer variable, which holds the address of C, A is a two-level pointer variable, which holds the address of B, then the 3 variables in memory occupy their respective storage units, the correlation between them , the relationship between the front and back positions is not important. At this point, B is a first-level pointer variable, the value of B (that is, the address of C) is a first-level pointer data, A is a two-level pointer variable, a value (that is, the address of B) is a level two pointer data, such as:


Below is a detailed introduction to level two pointers:




Floating pointer and wild pointer level two pointer in C + +

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.