Pointers on C——11 Dynamic Memory Allocation.2

来源:互联网 发布:金山软件股价 编辑:程序博客网 时间:2024/05/29 19:43

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

原创粉丝点击