| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 450 人关注过本帖
标题:[讨论]难度 编程 分享 一下 指教
只看楼主 加入收藏
w332004055
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-8-9
收藏
 问题点数:0 回复次数:1 
[讨论]难度 编程 分享 一下 指教

编写一个应用程序,其中创建一个有1024个元素的字符数组。每隔一个时间间隔产生一个1到20之间的随机整数,表示此时需要分配的内存空间的大小。设计相应的数据结构(例如空闲分区链或空闲分区表)记录这个数组空间的使用情况。采用首次适应算法、循环首次适应算法、最佳适应算法对这个数组空间进行分配。

#include <stdio.h>
#include <malloc.h>
#include <memory.h>
#include <stdlib.h>

//缓冲区大小
#define BUFFER_SIZE 1024
//缓冲区用来标记使用状态
#define USED 1
#define UNUSED 0
//产生随机数的时间
#define TIME_WAIT 2000

//要用来分配的缓冲区
char buffer[BUFFER_SIZE];

//状态表中元素的结构
struct state_tbl
{
char *position;
int size;
struct state_tbl *next;
bool used;
};

//初始化状态表
struct state_tbl *init()
{
struct state_tbl *head = (struct state_tbl *)malloc(sizeof(struct state_tbl));
head->position = buffer;
head->size = BUFFER_SIZE;
head->next = NULL;
head->used = false;
return head;
}

//销毁状态表
void destroy(struct state_tbl *head)
{
struct state_tbl *p;
struct state_tbl *tmp;
for(p=head ; p!=NULL ; p=tmp)
{
tmp = p->next;
free(p);
}
}

//修改状态表的内容

搜索更多相关主题的帖子: 难度 指教 分享 
2006-08-11 09:57
w332004055
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-8-9
收藏
得分:0 

写个C程序,花了一个小时,舍不得删,就存在这里好了

编写一个应用程序,其中创建一个有1024个元素的字符数组。每隔一个时间间隔产生一个1到20之间的随机整数,表示此时需要分配的内存空间的大小。设计相应的数据结构(例如空闲分区链或空闲分区表)记录这个数组空间的使用情况。采用首次适应算法、循环首次适应算法、最佳适应算法对这个数组空间进行分配。

#include <stdio.h>
#include <malloc.h>
#include <memory.h>
#include <stdlib.h>

//缓冲区大小
#define BUFFER_SIZE 1024
//缓冲区用来标记使用状态
#define USED 1
#define UNUSED 0
//产生随机数的时间
#define TIME_WAIT 2000

//要用来分配的缓冲区
char buffer[BUFFER_SIZE];

//状态表中元素的结构
struct state_tbl
{
char *position;
int size;
struct state_tbl *next;
bool used;
};

//初始化状态表
struct state_tbl *init()
{
struct state_tbl *head = (struct state_tbl *)malloc(sizeof(struct state_tbl));
head->position = buffer;
head->size = BUFFER_SIZE;
head->next = NULL;
head->used = false;
return head;
}

//销毁状态表
void destroy(struct state_tbl *head)
{
struct state_tbl *p;
struct state_tbl *tmp;
for(p=head ; p!=NULL ; p=tmp)
{
tmp = p->next;
free(p);
}
}

//修改状态表的内容
void modify(struct state_tbl *pre, int size)
{
struct state_tbl *p = (struct state_tbl *)malloc(sizeof(struct state_tbl));
p->position = pre->position + size;
p->size = pre->size - size;
p->next = pre->next;
p->used = false;
pre->next = p;
pre->size = size;
pre->used = true;
}

// first-fit 首次适配算法查找合适表项
struct state_tbl *find(struct state_tbl *head, int size_need)
{
struct state_tbl *p;
for(p=head ; p!=NULL ; p=p->next)
{
if((p->size >= size_need) && (p->used == false))
{
return p;
}
}
return NULL;
}

int main()
{

//标记缓冲区未使用
memset(buffer, UNUSED, BUFFER_SIZE);
struct state_tbl *head = init();
struct state_tbl *item;
int size;
while(1)
{
size = rand()%20;
printf("%d bytes will be alloc:",size);
item = find(head, size);
if(item == NULL)
{
printf("Not enough memory!\n");
break;
}
else
{
printf("successful\n");
modify(item, size);

memset(buffer, USED, size);
}

//delay(TIME_WAIT);
}
destroy(head);
return (0);
}


2006-08-11 10:00
快速回复:[讨论]难度 编程 分享 一下 指教
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020240 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved