Void * and NULL pointer

Source: Internet
Author: User
Tags define null

1. Void *

Void * is also called a generic pointer, that is, a pointer that can point to any type of data. When most users only regard a piece of memory as raw memeory, that is, when there is no type information, it is defined as void *, and the compiler processes it in one byte and one byte, similar to the unsigned char * processing method, of course, your char type must be 1 byte.
Operations on memory in the standard library of C <mem. h> almost all use the void * type. Convert the type to the programmer. Otherwise, the same function is provided only because the data types are different, C does not support function overloading.

In fact, can we think like this for C ++ and C? C's malloc only applies for a blank memory from the system and returns a pointer to this memory, then, nothing is done. What about C ++? The new A * A = new A (); in fact, C ++ has done three steps: first apply for memory, then execute the constructor on it, and finally assign the pointer to; since it is not an atomic operation, problems may arise. What problems? In order to improve the running speed of the CPU, the second and third steps of the code can actually be exchanged. Think of a multi-threaded code to determine whether a class pointer is null; --.. Far away.

Void (type) pointer is a special pointer that is smart enough to point to any data type address space. Of course, it also has certain limitations:

The '*' operator is used to retrieve the data in the address space indicated by the pointer. The programmer must understand that the void pointer cannot use this method to obtain the content indicated by the pointer. Because directly retrieving content is not allowed. However, you must convert the void pointer to any other pointer of the valid data type, such as char, Int, float, and so on before using '*' to retrieve the pointer content. This is the so-called type conversion concept.

 

So void * can be understood as the omnipotent Varible variable in scripting? It can be converted to any data type as needed. Of course, when the conversion format is incorrect, the corresponding content will be incorrect, and the memory access may even be out of bounds;

 

2. null

Null Pointer is a type of pointer of any data type. It is clear on the Windows platform

Stdio. h

/* Define NULL pointer value */
# Ifndef null
# Ifdef _ cplusplus
# Define null 0
# Else
# Define null (void *) 0)
# Endif
# Endif

If the C ++ compiler is defined as 0, otherwise it is defined as a pointer pointing to 0;

  1. # Include <iostream. h>
  2. Int* Exforsys;
  3. VoidMain ()
  4. {
  5. * Exforsys = 100;
  6. }

An error occurs. This is a classic error. exforsys does not apply for memory at all. How can I assign a value to him? So there is.

Exforsys = new int ();

I can int * exforsys = NULL; so I defined an integer pointer pointing to 0; void pointer is a special pointer that can point to any data type. The NULL pointer does not point to any valid address space or reference, but can be pointed to by any type 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.