1. 请自己尝试编写calloc函数,函数内部使用malloc函数来获取内存?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void *calloc( size_t num_elements, size_t element_size );
int main( void ){
size_t num_elements;
size_t element_size;
int *pi;
int *pi2;
int *pit;
num_elements = 10;
element_size = sizeof(int);
pi = (int *)calloc( num_elements, element_size );
pit = pi;
pi2 = pit + num_elements;
while( pit < pi2 ){
printf( "%d ", *pit++ );
}
/*
** free dynamic memory.
*/
free( pi );
return EXIT_SUCCESS;
}
void *calloc( size_t num_elements, size_t element_size ){
void *new_memory;
int i;
/*
** call malloc function to obtain the memory.
*/
new_memory = malloc( num_elements * element_size );
/*
** check the memory allocation is successful or failing.
*/
if( !new_memory ){
printf( "memory allocation fails.\n" );
return NULL;
}
/*
** set elements of memory to zero.
*/
memset( new_memory, 0, num_elements * element_size );
return new_memory;
}
/* 输出:

*/
2. 编写一个函数,从标准输入读取一列整数,把这些值存储于一个动态分配的数组中并返回这个数组。函数通过观察EOF来判断输入列表是否结束。数组的第1个数是数组包含的值的个数,它的后面就是这些整数值。
解析:
这个用于函数分配一个数组,并在需要时根据一个固定的增值对数组进行重新分配。增量DELTA可以进行微调,用于在效率和内存浪费之间进行一平衡。
/*
** 从标准输入读取一列由EOF结尾的整数并返回一个包含这些值的动态分配的数组。
** 数组的第1个元素是数组所包含的值的数量。
*/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define DELTA 100
int *readints( void );
int main( void ){
int *pi;
int i;
pi = readints();
for( i = 0; i <= pi[0]; ++i ){
printf( "%d ", pi[i] );
}
printf( "\n" );
free( pi );
return EXIT_SUCCESS;
}
int *readints( void ){
int *array;
int size;
int count;
int value;
/*
** 获得最初的数组,大小足以容纳DELTA个值。
*/
size = DELTA;
array = (int *)malloc( (size + 1) * sizeof(int) );
if( array == NULL ){
return NULL;
}
/*
** 从标准输入获得值。
*/
count = 0;
while( scanf( "%d", &value ) == 1 ){
/*
** 如果需要,使数组变大,然后存储这个值。
*/
count += 1;
if( count > size ){
size += DELTA;
array = (int *)realloc( array, (size + 1) * sizeof(int) );
if( array == NULL ){
return NULL;
}
}
array[count] = value;
}
/*
** 改变数组的长度,使其刚刚正好,然后存储并返回这个数组。
** 这样做绝不会使数组更大,所以它绝不会失败(但还是应该进行检查!)。
*/
if( count < size ){
array = (int *)realloc( array, (count + 1) * sizeof(int) );
if( array == NULL ){
return NULL;
}
}
array[0] = count;
return array;
}
/* 输出:

*/
3. 编写一个函数,从标准输入读取一个字符串,把字符串复制到动态分配的内存中,并返回该字符串的副本。这个函数不应该对读入字符的长度做任何限制。
#include <stdio.h>
#include <stdlib.h>
char *getstr( void );
int main( void ){
char *ps;
ps = getstr();
printf( "ps = %s\n", ps );
/*
** free dynamic memory.
*/
free( ps );
return EXIT_SUCCESS;
}
char *getstr( void ){
int ch;
int size;
int counter;
/*
** here, we assign 2 to size to avoid the condition of one value read.
** because we need to a extra space to store '\0', otherwise when only value
** is read, we don't have space to store '\0'.
*/
size = 2;
char *ps = (char *)malloc( size * sizeof(char) );
if( !ps ){
printf( "memory allocation fails.\n" );
return NULL;
}
*ps = '\0';
counter = 0;
while( (ch = getchar()) != EOF ){
ps[counter] = ch;
counter++;
if( counter == size - 1 ){
size *= 2;
/*
** note that realloc can copy previous elements of memory pointed to by previous pointer.
*/
ps = (char *)realloc( ps, size * sizeof(char) );
ps[counter] = '\0';
}
}
ps[counter] = '\0';
return ps;
}
/* 输出:

*/
4. 编写一个程序,按照下图所示创建数据结构。最后3个对象都是动态分配的结构。第1个对象可能是一个静态的指向结构的指针。
不必使这个程序过于全面---我们将在下一章讨论这个数据结构。
#include <stdio.h>
#include <stdlib.h>
struct Node{
int values;
struct Node *next;
};
int main( void ){
static struct Node *head;
struct Node *p;
struct Node *pnode, *pnode2, *pnode3;
pnode = (struct Node *)malloc( sizeof(struct Node) );
pnode2 = (struct Node *)malloc( sizeof(struct Node) );
pnode3 = (struct Node *)malloc( sizeof(struct Node) );
if( pnode && pnode2 && pnode3 ){
head = pnode;
pnode->values = 5;
pnode->next = pnode2;
pnode2->values = 10;
pnode2->next = pnode3;
pnode3->values = 15;
pnode3->next = NULL;
}
p = head;
while( p ){
printf( "%d ", p->values );
p = p->next;
}
printf( "\n" );
return EXIT_SUCCESS;
}
/* 输出:

*/