指针操作_1,指针基础Pointer Fundamentals与内存分配storage allocation

来源:互联网 发布:cohen sutherland算法 编辑:程序博客网 时间:2024/06/05 05:28

I 指针基础

a pointer is simply a variable that stores the address where a piece of data resides in memory rather than storing the data itself.

指针是存储存放在内存中的数据的地址的变量,而不是存放数据本身。
That is, pointers contain memory addresses.
也就是说,指针包含的是内存地址。

II 内存分配

It is also important to remember that when we declare a pointer, space is allocated only for the pointer itself; no space is allocated for the data the pointer references.
当申明一个指针的时候,就已经为这个指针分配了内存空间。但是,并没有为这个指针所指向的数据分配内存空间。
Storage for the data is allocated in one of two ways: by declaring a variable for it or by allocating storage dynamically at runtime (using malloc or realloc, for example).
为数据分配内存有两种方式:通过申明一个变量;或者,通过在运行时进行内存的动态分配。
When we declare a variable, its type tells the compiler how much storage to set aside for it as the program runs. 
当申明了一个变量后,那么,在程序运行的时候,它的类型将会告诉编译器要为这个变量留多大的内存。
Storage for the variable is allocated automatically, but it may not be persistent throughout the life of the program.
变量的内存是自动分配的,但是,这个内存可能不会在程序的生命周期内永久的存在。
This is especially important to remember when dealing with pointers toautomatic variables
在处理指针指向一个自动变量的问题的时候,这一点尤为重要。
Automatic variables are those for which storage is allocated and deallocated automatically when entering and leaving a block or function. 
自动变量,指的是,当程序进入或者离开一个函数块的时候,内存为它分配或者自动解除分配的变量。
<pre name="code" class="objc">int f(int **iptr){int a=10; //自动变量a*iptr=&a;//iptr设置为自动变量a的地址。当f返回时,iptr成了一个悬挂指针。因为当f返回的时候,a在程序堆栈中不再有效,//iptris set to the address of the automatic variablea in the functionf, iptr becomes a dangling pointer whenf returns. This//situation occurs because once f returns, a is no longer valid on the program stackreturn 0;}


In C, when we dynamically allocate storage, we get a pointer to some storage on the heap. Since it is then our responsibility to manage this storage ourselves, the storage remains valid until we explicitly deallocate it. 
c语言中,当我们动态分配内存的时候,我们就会获得一个堆上只想一定内存的指针。内存一直保持有效,直到我们明确的解除分配为止,我们又责任管理好这些内存。
For example, the storage allocated bymallocin the following code remains valid until we callfreeat some later time. Thus, it remains valid even aftergreturns, unlike the storage allocated automatically forapreviously. 
例如,通过malloc分配的内存是一直有效的,直到在后面某个时候我们调用free函数为止。因此,即使g返回了,malloc分配的内存还是一直有效。这个与前面所讲的自动分配内存(a)是不一样的。
#include <stdio.h>int g(int **iptr){if(((*iptr)=(int *)malloc(sizeof(int)))==null)return -1;return 0;}
The parameteriptris a pointer to the object we wish to modify (another pointer) so that whengreturns,iptr contains the address returned bymalloc.
参数iptr 是一个指向我们想要修改的对象(另外一个指针)的指针,因此当g返回的时候,iptr里面存放的还是malloc返回的地址。



The misuse of dynamically allocated storage, in particular, is a notorious source ofmemory leaks. Memory leaks are blocks of storage that are allocated but never freed by a program, even when no longer in use. 
错误的使用动态内存分配,最为严重的后果是造成内存泄露。内存泄露是指,一个程序分配了内存,但是一直都没有释放内存,即使是这个内存块之后都没有被使用。
















0 0
原创粉丝点击