The wild pointer in C-how to avoid

Source: Internet
Author: User

Transferred from: http://www.cnblogs.com/viviwind/archive/2012/08/14/2638810.html

Let's look at an example:

struct student
{
char* name;
int score;
}stu, *pstu;

int main (void)
{
strcpy (Stu.name, "Jimy");
Stu.score = 99;
Return0;
}

This is a lot of people are prone to make mistakes: define the structure of the variable Stu, but the structure of the char * name in the definition of the structure is only allocated 4 bytes of memory, does not point to a legitimate address, when its pointer to the area is only some garbled, there is no access permission. This is what we usually call a wild pointer, which is the source of many bugs in the program.

To avoid a wild pointer, we usually need to initialize the pointer to null, and then assign it a value of NULL when it is exhausted.

Struct member pointers tend to be overlooked by us, notice that it also needs to be initialized, and that you need to allocate enough memory for the struct pointer, so you can use malloc as a macro, using the following method:

Assigning an initial value to a pointer variable:

Method One: char * p = (char *) malloc (sizeof (char));

Method Two: char * p = NULL;

Assigning an initial value to an array:

Method one: int a[10] = {0};

Method Two: memset (A, 0, sizeof (a));

The Memset function has 3 parameters: the first is the memory start address to be set, the second is the value to be set, and the third is the memory size to be set, in bytes;

Assigning an initial value to a struct pointer:

Method One: Pstu = (struct student *) malloc (sizeof (struct student));

Common error: Pstu = (struct student *) malloc (sizeof (struct student *));

In order to avoid the presence of wild pointers, the debug version of the program can be handled as follows:

Use ASSERT (NULL! = p) At the entrance of the function to validate the parameter. or use if (NULL! = p) to validate. It will remind us that there are no initialization pointers and functions to locate errors.

Assert is a macro, and if the condition in parentheses behind it is not met, the program terminates and prompts for an error.

Be sure to release the memory that the pointer points to when you're done with the pointer, or you don't know when we've changed the value of the pointer to make it into a wild pointer!

This requires the help of the free (p) function, which can cut off the pointer-to-memory link. However, it is important to note that free can only be used once for the same pointer variable. This can result in an error or a memory leak.

After releasing memory, the value of the other pointer changes to null!

As follows:

Free (p);

p = NULL;

The wild pointer in

C-how to avoid

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.