C语言实现连续分配,链接分配,索引分配等三种分配方式可运行代码
时间: 2023-08-06 17:04:10 浏览: 104
以下是C语言实现三种内存分配方式的代码示例:
1. 连续分配
```c
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 1000 // 内存大小
int memory[MAXSIZE]; // 内存空间
int cursor = 0; // 游标
// 连续分配内存
int contiguous_allocation(int size) {
int start = cursor; // 起始地址
cursor += size; // 游标后移
if (cursor > MAXSIZE) { // 内存不足
printf("Memory overflow!\n");
return -1;
}
return start;
}
int main() {
int p1, p2, p3;
p1 = contiguous_allocation(10);
p2 = contiguous_allocation(20);
p3 = contiguous_allocation(30);
printf("p1 = %d\np2 = %d\np3 = %d\n", p1, p2, p3);
return 0;
}
```
2. 链接分配
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int size; // 分区大小
int start; // 分区起始地址
struct node *next; // 下一个分区
} Node;
Node *head = NULL; // 首分区
// 链接分配内存
int linked_allocation(int size) {
Node *p = head;
while (p) {
if (p->size >= size) { // 找到合适的分区
int start = p->start;
if (p->size == size) { // 分区大小刚好
head = p->next;
} else { // 分区大小超出需求
p->size -= size;
p->start += size;
}
return start;
}
p = p->next;
}
printf("Memory overflow!\n"); // 内存不足
return -1;
}
// 将分区插入空闲分区链表中
void insert(Node *node) {
Node *p = head, *q = NULL;
while (p && p->start < node->start) {
q = p;
p = p->next;
}
if (q == NULL) { // 插入首部
node->next = head;
head = node;
} else { // 插入中间或尾部
node->next = q->next;
q->next = node;
}
}
// 回收分区
void free_memory(int start, int size) {
Node *p = (Node *)malloc(sizeof(Node));
p->size = size;
p->start = start;
insert(p);
}
int main() {
int p1, p2, p3;
Node *p;
p1 = linked_allocation(10);
p2 = linked_allocation(20);
p3 = linked_allocation(30);
printf("p1 = %d\np2 = %d\np3 = %d\n", p1, p2, p3);
free_memory(p2, 20); // 回收分区
p = head;
while (p) { // 显示空闲分区链表
printf("start = %d, size = %d\n", p->start, p->size);
p = p->next;
}
return 0;
}
```
3. 索引分配
```c
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100 // 内存大小
#define MAXINDEX 10 // 索引表大小
int memory[MAXSIZE]; // 内存空间
int index_table[MAXINDEX]; // 索引表
int cursor = 0; // 游标
// 索引分配内存
int indexed_allocation(int size) {
int i, j, k, start;
i = cursor / MAXINDEX; // 索引表号
j = cursor % MAXINDEX; // 索引表内偏移量
start = i * MAXINDEX + j; // 起始地址
if (start + size > (i + 1) * MAXINDEX) { // 空间不足
for (k = j; k < MAXINDEX; k++) { // 从当前索引表开始查找
if (index_table[i] + k >= MAXSIZE) { // 内存溢出
break;
}
if (!memory[index_table[i] + k]) { // 找到空闲块
start = i * MAXINDEX + k; // 更新起始地址
cursor = start; // 更新游标
for (j = 0; j < size; j++) { // 标记已使用
memory[start + j] = 1;
}
return start;
}
}
for (i = 0; i < MAXINDEX; i++) { // 查找整个索引表
for (j = 0; j < MAXINDEX; j++) {
if (index_table[i] + j >= MAXSIZE) { // 内存溢出
break;
}
if (!memory[index_table[i] + j]) { // 找到空闲块
start = i * MAXINDEX + j; // 更新起始地址
cursor = start; // 更新游标
for (k = 0; k < size; k++) { // 标记已使用
memory[start + k] = 1;
}
return start;
}
}
}
printf("Memory overflow!\n"); // 内存不足
return -1;
} else { // 空间充足
cursor += size; // 更新游标
for (k = 0; k < size; k++) { // 标记已使用
memory[start + k] = 1;
}
return start;
}
}
int main() {
int p1, p2, p3;
index_table[0] = 0; // 第一个索引表起始位置为0
p1 = indexed_allocation(10);
p2 = indexed_allocation(20);
p3 = indexed_allocation(30);
printf("p1 = %d\np2 = %d\np3 = %d\n", p1, p2, p3);
return 0;
}
```