1、请你尝试编写calloc函数,函数内部使用malloc函数来获取内存。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void *mycalloc(size_t nmemb, size_t size);
void main(void)
{
char *p;
p = mycalloc(10, sizeof(char));
printf("calloc: %s\n", p);
}
void *mycalloc(size_t nmemb, size_t size)
{
void *pm;
if (nmemb == 0 || size == 0)
return NULL;
pm = malloc(nmemb * size);
if (pm == NULL)
return NULL;
memset(pm, 0, nmemb * size);
return pm;
}
2、编写一个函数,从标准输入读取一列整数,把这些值存储于一个动态分配的数组中并返回这个数组,函数通过观察EOF判断输入是否结束,数组的第一个数是数组包含的值的个数,它的后面就是这些整数值。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DELTA 100
int *read();
void main()
{
int *p;
p = read();
free(p);
}
int *read()
{
int count;
int value;
int *array;
int size;
count = 0;
size = DELTA;
array = malloc((size + 1) * sizeof(int));
if (array == NULL)
return NULL;
printf("please input some number: ");
while (scanf("%d", &value)) {
if (value == 0)
break;
array[++count] = value;
if (count >= size) {
size += DELTA;
array = realloc(array, (size + 1) * sizeof(int));
if (array == NULL)
return NULL;
}
}
if (count < size) {
array = realloc(array, (count + 1) * sizeof(int));
if (array == NULL)
return NULL;
}
array[0] = count;
printf("number count: %d\n", array[0]);
for (int i = 1; i <= count; i++)
printf("number %d: %d\n", i, array[i]);
return array;
}
3、编写一个函数,从标准输入读取一个字符串,把字符串复制到动态内存分配的内存中,并返回该字符串的拷贝,这个函数不应该对读入字符串的长度作任何限制。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DELTA 100
char *readstr();
void main()
{
char *string;
if (string = readstr())
printf("read string success: %s\n", string);
else
printf("read string fail");
free(string);
}
char *readstr()
{
char ch;
int count;
char *str;
int size;
count = 0;
size = DELTA;
str = malloc(size * sizeof(char));
if (str == NULL)
return NULL;
printf("please input string: ");
while ((ch = getchar()) != '\n') {
str[count++] = ch;
if (count >= size) {
size += DELTA;
str = realloc(str, size * sizeof(char));
if (str == NULL)
return NULL;
}
}
if (size < count) {
str = realloc(str, count *sizeof(char));
if (str == NULL)
return NULL;
}
return str;
}
4、编写一个程序,创建一个链表,包含一个链表头和三个动态分配的对象。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct list{
int data;
struct list *next;
} List;
List *create_list(int count);
void print_list(List *head);
int main(int argc, char **argv)
{
static List *head;
if (argc != 2) {
printf("Error! Please input 2 parameter.\n");
return 0;
}
head = create_list(atoi(argv[1]));
if (head == NULL)
printf("Create list fail.\n");
else {
printf("Create list seccess.\n");
print_list(head);
}
return 0;
}
List *create_list(int count)
{
int i;
List *head;
List *next;
if (count <= 0)
return NULL;
head = malloc(sizeof(List));
if (head == NULL)
return NULL;
head->data = 5;
next = head;
for (i = 1; i < count; i++) {
next->next = malloc(sizeof(List));
next = next->next;
if (next == NULL)
return NULL;
next->data = 5 * (i + 1);
}
next = NULL;
return head;
}
void print_list(List *head)
{
List *next;
printf("list: %d", head->data);
next = head->next;
while (next != NULL) {
printf("->%d", next->data);
next = next->next;
}
printf("\n");
}