SJTU->SE->ICS->LAB6 Malloc

这篇博客记录了作者在SJTU计算机科学课程ICS中的第六次实验室作业,主要内容是实现malloc函数。经过努力,作者的代码在mdriver测试中获得了57/60的分数,而在mdriver -V测试中则得到了92/100的好成绩。博客分享了这一过程和代码实现。
摘要由CSDN通过智能技术生成

这个=。=全是泪,不说了,写下来的结果似乎是57/60,已经不错了那=。=

至于mdriver测试如下

mdriver -V


92/100

好了,废话不多说了,代码如下

/* 
 * mm.c
 *the data structure is explict list,and use the binary tree
 *
 */  
#include <stdio.h>  
#include <stdlib.h>  
#include <assert.h>  
#include <unistd.h>  
#include <string.h>  
  
#include "mm.h"  
#include "memlib.h"  
 
team_t team = {
	
};

#define WSIZE 4 
#define DSIZE 8 

//max,you know it~.~
#define MAX(x,y) ((x)>(y)? (x): (y))

//get and put the value from the address p~.~
#define GET(p) (*(size_t *)(p))
#define PUT(p,val) (*(size_t *)(p)=(val))

//get the size of the block from the header p of the block~.~
#define SIZE(p) ((GET(p))&~0x7)
#define PACK(size,alloc) ((size)|(alloc))

//get everyting from the block bp~.~
#define ALLOC(bp) (GET(bp)&0x1)
#define HEAD(bp) ((void *)(bp)-WSIZE)
#define LEFT(bp) ((void *)(bp))
#define RIGHT(bp) ((void *)(bp)+WSIZE)
#define PRNT(bp) ((void *)(bp)+DSIZE)
#define BROS(bp) ((void *)(bp)+(3*WSIZE))
#define FOOT(bp) ((void *)(bp)+SIZE(HEAD(bp))-DSIZE)

//get the size aligned~.~
#define ALIGNED(size) (((size) + 0x7) & ~0x7)

//get the size or the allocness of the block bp~.~
#define GET_HEAD_SIZE(bp) SIZE(HEAD(bp))
#define GET_HEAD_ALLOC(bp) (GET(HEAD(bp))&0x1)

//get the previous or next block~.~
#define PREV_BLKP(bp) ((void *)(bp)-SIZE(((void *)(bp)-DSIZE)))
#define NEXT_BLKP(bp) ((void *)(bp)+GET_HEAD_SIZE(bp))

//get or set the left or right child of bp~.~
#define PUT_LEFT_CHILD(bp,val) (PUT(LEFT(bp),(int)val))
#define PUT_RIGHT_CHILD(bp,val) (PUT(RIGHT(bp),(int)val))
#define GET_LEFT_CHILD(bp) (GET(LEFT(bp)))
#define GET_RIGHT_CHILD(bp) (GET(RIGHT(bp)))

//get or set the head or foot of bp~.~
#define PUT_HEAD(bp,val) (PUT(HEAD(bp),(int)val))
#define PUT_FOOT(bp,val) (PUT(FOOT(bp),(int)val))
#define PUT_PACK_HEAD(bp,size,alloc) (PUT_HEAD(bp,PACK(size,alloc)))
#define PUT_PACK_FOOT(bp,size,alloc) (PUT_FOOT(bp,PACK(size,alloc)))
#define GET_HEAD(bp) (GET(HEAD(bp)))
#define GET_FOOT(bp) (GET(FOOT(bp)))

//get the parent or brother of the block bp~.~
#define PUT_PAR(bp,val) (PUT(PRNT(bp),(int)val))
#define PUT_BROS(bp,val) (PUT(BROS(bp),(int)val))
#define GET_PAR(bp) (GET(PRNT(bp)))
#define GET_BRO(bp) (GET(BROS(bp)))

int mm_init ();
void *mm_malloc (size_t size);
void mm_free (void *bp);
void *mm_realloc (void *bp,size_t size);

//declear the function and variable~.~
static void *coalesce (void *bp);
static void *extend_heap (size_t size);
static void place (void *ptr,size_t asize);
static void delete_node (void *bp);
static void add_node (void *bp);
static void *find_fit (size_t asize);
static void check(void *bp);
static void mm_check();

static void *heap_list_ptr = 0;
static void *my_tree = 0;
static size_t flag = 0;

//check one address
static void check(void *bp)
{
	printf("0.the adress is 0x%x\n",bp);
	printf("1.the size of the block is %d\n",GET_HEAD_SIZE(bp));
	printf("2.the allocness of the block is %d\n",GET_HEAD_ALLOC(bp));
	printf("-1.the size of next block is %d\n",GET_HEAD_SIZE(NEXT_BLKP(bp)));
	printf("-2.the allocness of next block is %d\n",GET_HEAD_ALLOC(NEXT_BLKP(bp)));
	printf("0x%x[%d/%d] ~ 0x%x[%d/%d]\n",HEAD(bp),GET_HEAD_SIZE(bp),GET_HEAD_ALLOC(bp)\
	,FOOT(bp),GET_HEAD_SIZE(bp),GET_HEAD_ALLOC(bp));
	printf("0x%x[%d/%d] ~ 0x%x[%d/%d]\n\n",HEAD(NEXT_BLKP(bp)),\
	GET_HEAD_SIZE(NEXT_BLKP(bp)),GET_HEAD_ALLOC(NEXT_BLKP(bp))\
	,FOOT(bp),GET_HEAD_SIZE(NEXT_BLKP(bp)),GET_HEAD_ALLOC(NEXT_BLKP(bp)));
}

//check all the heap
static void mm_check()
{
	void *buff = 0;
	buff = heap_list_p
编写一个C程序来实现这个功能,我们可以使用递归的方式来分割链表,并对每个部分进行反转。首先,我们需要创建一个链表节点结构体`ListNode`,然后定义辅助函数`reverseList`来反转链表。主函数`splitAndReverse`会完成整个操作。 ```c #include <stdio.h> #include <stdlib.h> // 链表节点结构体 typedef struct ListNode { int data; struct ListNode* next; } ListNode; // 反转链表的辅助函数 ListNode* reverseList(ListNode* head) { ListNode* prev = NULL; ListNode* curr = head; while (curr != NULL) { ListNode* nextTemp = curr->next; curr->next = prev; prev = curr; curr = nextTemp; } return prev; } // 主函数:划分并反转链表 ListNode* splitAndReverse(ListNode* head, int k) { if (head == NULL || k <= 0) { return head; } // 计算剩余节点数,如果不满k个则停止递归 int remainingNodes = 0; ListNode* temp = head; while (temp != NULL) { remainingNodes++; temp = temp->next; if (remainingNodes >= k) { break; } } // 如果剩余节点数能构成一段完整链表,反转当前部分 if (remainingNodes > 0) { ListNode* reversed = reverseList(head); splitAndReverse(reversed, k - 1); // 对剩余部分继续递归 head->next = reversed; // 将反转后的链表连接到原链表 head = reversed; } return head; } // 测试案例 void printList(ListNode* head) { while (head != NULL) { printf("%d -> ", head->data); head = head->next; } printf("NULL\n"); } int main() { // 创建示例链表:1->2->3->4->5->6 ListNode* list = (ListNode*)malloc(sizeof(ListNode)); list->data = 1; list->next = (ListNode*)malloc(sizeof(ListNode)); list->next->data = 2; list->next->next = (ListNode*)malloc(sizeof(ListNode)); list->next->next->data = 3; list->next->next->next = (ListNode*)malloc(sizeof(ListNode)); list->next->next->next->data = 4; list->next->next->next->next = (ListNode*)malloc(sizeof(ListNode)); list->next->next->next->next->data = 5; list->next->next->next->next->next = (ListNode*)malloc(sizeof(ListNode)); list->next->next->next->next->next->data = 6; list->next->next->next->next->next->next = NULL; // 分割并打印结果 int k = 3; list = splitAndReverse(list, k); printList(list); k = 4; list = splitAndReverse(list, k); printList(list); return 0; } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值