The basic concept of a pointer is simple: it is a variable that stores the address of a memory location.
The key to comprehending pointers is understanding how memory is managed in a C program.
【指针是一个变量,存的是一块内存空间的地址,在C中根据指针的声明,取不同size的内存空间,在C#中内存空间额外存放了两个内容,其中一个可以判断取出来的类型是否正确。】
There are various types of “nulls” supported by C so a careful examination of them can be enlightening.
The memory model used with a given compiler and operating system environment affects how pointers are used. 【不同编译器和不同操作系统环境】
When a C program is compiled, it works with three types of memory: Static/Global (be allocated to this type of memory), Automatic (local) and Dynamic (be allocated from the heap and can be released as necessary).
A pointer variable contains the address in memory of another variable, object, or function. Nothing inherent in a pointer indicates what type of data the pointer is referencing. A pointer only contains an address.
Providing the ability to pass data structures by pointer without incurring a large overhead. 【C# via CLR中建议小结构用structure,大结构用reference,也是基于这个理由】
很多数据结构可以用pointer也可以用array实现,pointer更加灵活。The size of an array typically needs to be known when it is created. The pointer representation does not suffer from this limitation as a new node can be dynamically allocated as needed.
The malloc and free functions are used to allocate and release dynamic memory, respectively. Dynamic memory allocation enables variable-sized arrays and data structures. C++11已经支持variable size arrays.
There are situations where the C specification does not explicitly define pointer behavior: 1. Implementation-defined (means that the implementation is free to do what it likes, but must document its choice and stick to it.), Unspecified and Undefined. 【Q:查阅】
int num;
int *pi;
variable, address, uninitialized memory
【目前分配都在stack上,pointer是variable,只是里面存的是address】
The trick is to read pointer declarations backward.
const int *pci;
P10有一段关于Virtual memory and pointers。
The address of operator, &, will return its operand’s address. The indirection operator, *, returns the value pointed to by a pointer variable. A pointer can be declared to point to a function. The function is passed void and returns void. The pointer’s name is foo.
void (*foo)();
The null concept: refers to the idea that a pointer can hold a special value that is not equal to another pointer. It does not point to any area of memory.
The null pointer constant: The null concept is an abstraction supported by the null pointer constant. This constant may or may not be a constant zero.
The NULL macro: a constant integer zero cast to a pointer to void. In many libraries, #define NULL ((void *)0).
The ASCII NUL is defined as a byte containing all zeros. However, this is not the same as a null pointer. A string in C is represented as a sequence of characters terminated by a zero value. The null string is an empty string and does not contain any characters. Finally, the null statement consists of a statement with a single semicolon.引自 Introduction
A pointer containing NULL does not reference any location in memory. A null pointer should never be dereferenced because it does not contain a valid address.We can assign a zero to a pointer, but we cannot assign any other integer value.
A pointer can be used as the sole operand of a logical expression.
if(pi) {
// Not NULL
} else {
// Is NULL
}
void *pv; A pointer to void is a general-purpose pointer used to hold references to any data type.Pointers to void are used for data pointers, not function pointers. 【polymorphic, P194】
A pointer to void will have the same representation and memory alignment as a pointer to char. A pointer to void will never be equal to another pointer. However, two void pointers assigned a NULL value will be equal.
If a pointer is declared as global or static, it is initialized to NULL when the program starts.
int *globalpi;
void foo() {
static int *staticpi;
...
}
int main() {
...
}
Stack frames are pushed onto the stack, and the heap is used for dynamic memory allocation. The region above the heap is used for static/global variables. Static and global variables are frequently placed in a data segment separate from the data segment used by the stack and heap.