An in-depth analysis of wild pointers based on C language

Source: Internet
Author: User
Tags printf valid
"Wild Pointer" is not a null pointer, it is a pointer to "junk" memory. People generally do not use null pointers incorrectly, because it is easy to judge with an if statement. But "wild pointers" are dangerous, and if statements don't work for it

There are two main causes of the "wild pointer":
(1) The pointer variable has not been 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 scrambled. Therefore, the pointer variable should be initialized at the time it is created, either by setting the pointer to null or by having it point to legitimate memory. For example

Copy Code code as follows:


char *p = NULL;


char *str = (char *) malloc (100);


(2) when the pointer p is free or delete, it is not set to NULL, and the person mistakenly thinks that p is a valid pointer. See section 7.5.
Although the names of free and delete are vicious (especially delete), they simply release the memory that the pointer refers to, but do not kill the pointer itself.

Using the debugger to trace example 7-5, found that the pointer p is free after the address is still unchanged (not null), but the corresponding memory of the address is garbage, p became a "wild pointer." If P is not set to null at this time, it can be mistaken for a p to be a valid pointer.

If the program is longer, we sometimes cannot remember whether the memory referred to by P has been released, and the statement if (p!= NULL) is usually used for error proofing before continuing with P. Unfortunately, the IF statement does not work correctly, because even if p is not a null pointer, it does not point to a valid block of memory.

Copy Code code as follows:


char *p = (char *) malloc (100);


strcpy (P, "hello");


Free (p); The memory referred to by P is released, but the address that P refers to remains unchanged


...


if (P!= NULL)//does not play a role in error proofing


     {


strcpy (P, "World"); Error


}


example 7-5 p becomes wild pointer
(3) The pointer operation goes beyond the scope of the variable. This is an impossible situation and the sample program is as follows:

Copy Code code as follows:


class A


{     


Public:


void Func (void) {cout << "Func of Class A" << Endl;}


};


void Test (void)


{


A *p;


{


A;


p = &a; Note the life cycle of a


}


P->func (); P is "wild pointer"


}


When the function test executes the statement P->func (), object A disappears and P points to a, so p becomes a "wild pointer". But the weird thing is that I didn't make a mistake when I ran the program, which might have something to do with the compiler.
Instance Program:

Copy Code code as follows:


#include <stdio.h>


#include <stdlib.h>


int main (int argc, char *argv[])


{


char *p = NULL;


p = (char*) malloc (sizeof (char) *100);


printf ("Pointer P's address is:%PN", p);


strcpy (P, "Hello");


printf ("%sn", p);


free (p);





printf ("Pointer P's address is:%PN", p);





System ("PAUSE");


return 0;


}


Run the screenshot as follows:



It can be seen that, while using free (p), the address space that the P points to is freed, but the pointer still exists and only points to "garbage" memory.
At this point P's state is called "Wild pointer"

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.