Problems caused by wild pointers

Source: Internet
Author: User

Today, when I ran the program, I encountered a very strange problem. After I added a few prints, it would be a crash. It would be strange to get rid of it and run normally, why did this happen? Finally, I had to think about it, and asked my colleagues to come and look at it. Finally, I found it was a problem caused by a wild pointer.

Next, we will give a brief introduction to the wild pointer.

Definition:

The "wild pointer" is not a null pointer, but a pointer to the "junk" memory. Generally, null pointers are not incorrectly used, because if statements are easy to judge. However, the "wild Pointer" is very dangerous, and the IF statement does not work for it.

Cause:

There are three main causes of the wild pointer:

I. pointer variables have not been initialized. When a pointer variable is created, it does not automatically become a null pointer. Its default value is random, which means it is random. Therefore, the pointer variable should be initialized at the same time as it is created, or be directed to the valid memory.

2. After the pointer P is free or deleted, It is not set to null. It is mistaken that p is a valid pointer. Don't look at the bad names of free and delete (especially delete). They just release the memory indicated by the pointer, but didn't kill the pointer itself. Normally, if (P! = NULL. Unfortunately, the IF statement does not prevent errors at this time, because even if p is not a null pointer, it does not point to a valid memory block. Example:

Char * P = (char *) malloc (100 );

Strcpy (P, "hello ");

Free (p); // The Memory indicated by P is released, but the address indicated by P remains unchanged.

If (P! = NULL) // does not prevent errors

Strcpy (p, "World"); // Error

Another issue to be noted: Do not return a pointer or reference pointing to the stack memory, because the function in the stack will be released when it ends.

3. pointer operations go beyond the scope of variables. This situation is hard to prevent.

# Include <stdio. h> <br/> 2 void test () <br/> 3 {<br/> 4 printf ("test/N "); <br/> 5} <br/> 6 int main () <br/> 7 {<br/> 8 struct a <br/> 9 {<br/> 10 void (* func) (void ); <br/> 11 <br/> 12 }; <br/> 13 struct a * P; <br/> 14 {<br/> 15 struct a = {. func = test }; <br/> 16 <br/> 17 p = & A; <br/> 18} <br/> 19 P-> func (); // wild pointer: <br/> 20} <br/> 21 <br/> ~

So be careful when using pointers in the future. Either point it to null or malloc to point it to an available space.

 

By the way, we will introduce the malloc function.

Malloc requests the system to allocate a specified size of memory space. The return type is void. Void * Indicates a pointer of unknown type. C, C ++ stipulates that the void * type can be forcibly converted to any other type of pointer.

Prototype: extern void * malloc (unsigned int num_butes );

Header file: stdlib. h

Function: allocate memory blocks of num_bytes bytes.

Return Value: if the allocation is successful, the pointer pointing to the allocated memory is returned. Otherwise, the NULL pointer is returned. When the memory is no longer used, use the free () function to release the memory block.

Description: malloc applies to the system for allocating a specified size of bytes of memory. The return type is void. Void * Indicates a pointer of unknown type. C, C ++ stipulates that the void * type can be forcibly converted to any other type of pointer.

We can see from the function declaration. There are at least two differences between malloc and new: New returns a pointer of the specified type and can automatically calculate the required size. For example:

Int * P;

P = new int; // The return type is int * (integer pointer) and the allocated size is sizeof (INT );

Or:

Int * Parr;

Parr = new int [100]; // The returned type is int * (integer pointer) and the allocated size is sizeof (INT) * 100;

While malloc must be calculated by the number of bytes and forcibly converted to the actual type of pointer after the return.

Int * P;

P = (int *) malloc (sizeof (INT ));

First, the malloc function returns the void * type. If you write it as P = malloc (sizeof (INT), the program cannot be compiled and an error is returned: "You cannot assign void * to int * type variables ". Therefore, you must use (int *) to forcibly convert the data.

Second, the real parameter of the function is sizeof (INT), used to specify the size required for an integer data. If you write:

Int * P = (int *) malloc (1 );

The code can also be compiled, but in fact only one byte memory space is allocated. When you store an integer in it, three bytes will be left homeless, and you can directly "Live in the neighbor's house "! The result is that all the original data in the memory is cleared.

Malloc can also achieve the effect of new [], and apply for a continuous memory, the method is nothing more than specify the memory size you need.

For example, you want to allocate 100 int-type space:

Int * P = (int *) malloc (sizeof (INT) * 100); // allocate memory space that can be placed in the next 100 integers.

Another difference that cannot be seen directly is that malloc only allocates memory and Cannot initialize the obtained memory. Therefore, the value of a new memory will be random.

Add a special case to it

Char * PTR;

If (PTR = (char *) malloc (0) = NULL)

Puts ("got a null pointer ");

Else

Puts ("got a valid Pointer ");

In this case, got a valid pointer is obtained. Assign 0 to malloc to obtain a valid 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.