11.3 Calloc and Realloc
There are two additional memory allocation functions, calloc and realloc. Their prototypes are shown below.
另外還有兩個記憶體配置設定函數, calloc 和realloc。 它們的原型如下所示:
void *calloc( size_t num_elementa,size_t element_size );
void *realloc( void *ptr, size_t new_size );
calloc also allocates memory. The major difference between malloc and calloc is that the latter initializes the memory to zero before returning a pointer to it. This initialization is often convenient, but is a waste of time if the first thing your program does is to store values into the array. A minor difference between calloc and malloc is the way the amount of memory is requested, calloc takes the number of elements desired and the number of bytes in each element. From these values it computes the total number of bytes needed.
calloc 也用于配置設定記憶體。malloc 和calloc 之間的主要差別是後者在傳回指向記憶體的指針之前把它初始化為0 。這個初始化常常能帶來友善,但如果你的程式隻是想把一些值存儲到數組中,那麼這個初始化過程純屬浪費時間。calloc 和malloc 之間另一個較小的差別是它們請求記憶體數量的方式不同。calloc 的參數包括所需元素的數量和每個元素的位元組數。根據這些值,它能夠計算出總共需要配置設定的記憶體。
The realloc function is used to change the size of a previously allocated block of memory. You can make a block larger or smaller with this function If a block is made larger, its old contents remain unchanged and additional memory is added to the end of the block. The new memory is not initialized in any way. If the block is made smaller, then memory is taken off of the end. What remains of the original contents are unchanged.
realloc 函數用于修改一個原先已經配置設定的記憶體塊的大小。使用這個函數,你可以使一塊記憶體擴大或縮小。如果它用于擴大一個記憶體塊,那麼這塊記憶體原先的内容依然保留,新增加的記憶體添加到原先記憶體塊的後面,新記憶體并未以任何方法進行初始化。如果它用于縮小一個記憶體塊,該記憶體塊尾部的部分記憶體便被拿掉,剩餘部分記憶體的原先内容依然保留。
If the original block cannot be resized, realloc will allocate a different block of the right size and copy the contents of the old block to the new one. Thus, you must not use the old pointer to the block after a call to realloc. Use the new pointer that is returned instead.
如果原先的記憶體塊無法改變大小, realloc 将配置設定另一塊正确大小的記憶體,并把原先那塊記憶體的内容複制到新的塊上。是以,在使用realloc 之後,你就不能再使用指向舊記憶體的指針,而是應該改用realloc 所傳回的新指針。
Finally, if the first argument to realloc is NULL, then it behaves exactly like malloc.
最後,如果realloc 函數的第1 個參數是NULL.那麼它的行為就和malloc 一模一樣。
11.4 Using Dynamically Allocated Memory
Here is an example that obtains a chunk of memory from malloc.
這裡有一個例子,它用malloc 配置設定一塊記憶體。
int *pi;
...
pi = malloc( 100 );
if( pi == NULL ){
printf( "Out of memory!\n" );
exit( 1 );
}
The symbol NULL is defined in stdio.h as the literal constant zero. It acts as a visual reminder that the value being tested is a pointer type rather than an integer.
符号NULL 定義于stdio.h ,它實際上是字面值常量0 。它在這裡起着視覺提醒器的作用,提醒我們進行測試的值是一個指針而不是整數。
If there was memory available, we will now have a pointer to 100 bytes. On a machine with 4‐byte integers, the memory will be treated as an array of 25 integers because pi is a pointer to an integer.
如果記憶體配置設定成功,那麼我們就擁有了一個指向100 個位元組的指針。在整型為4 個位元組的機器上,這塊記憶體将被當作25 個整型元素的數組,因為pi 是一個指向整型的指針。
If your goal is to get enough memory for 25 integers, though, here is a much better technique for obtaining it.
但是,如果你的目标就是獲得足夠存儲25 個整數的記憶體,這裡有一個更好的技巧來實作這個目的。
pi = malloc( 25 * sizeof( int ) );
This approach is better because it is portable. It works properly even on machines with different size integers.
這個方法更好一些,因為它是可移植的。即使是在整數長度不同的機器上,它也能獲得正确的結果。
Now that you have a pointer, how do you use the memory? Of course you can use indirection and pointer arithmetic to access different integer locations in this array,as in this loop, which sets each element of the newly allocated array to zero:
既然你已經有了一個指針,那麼你該如何使用這塊記憶體呢?當然,你可以使用間接通路和指針運算來通路數組的不同整數位置,下面這個循環就是這樣做的,它把這個新配置設定的數組的每個元素都初始化為0:
int *pi2, i;
...
pi2 = pi;
for( i = 0; i < 25; i += 1 )
*pi2++ = 0;
As you have seen, you can use a subscript on the pointer as well. This second loop performs the same work as the previous one.
正如你所見,你不僅可以使用指針,也可以使用下标。下面的第2 個循環所執行的任務和前面一個相同。
int i;
...
for( i = 0; i < 25; i += 1 )
pi[i] = 0;
上一章 Pointers on C——11 Dynamic Memory Allocation.1