gpt4 book ai didi

c++ - C++中的C函数

转载 作者:行者123 更新时间:2023-11-28 05:50:09 24 4
gpt4 key购买 nike

<分区>

#include <iostream>
#include <sstream>
#include "blocknode.h"

using namespace std;

class MemoryManager
{
public:
MemoryManager(unsigned int memsize);
unsigned char * malloc(unsigned int request);
void free(unsigned char * blockptr);
blocknode *getFirstPtr();
friend ostream & operator<<(ostream & out,const MemoryManager &M);

private:
unsigned int memsize;
unsigned char *baseptr;
blocknode * firstBlock;

void mergeForward(blocknode *p);
void splitBlock(blocknode *p,unsigned int chunksize);
};

这是 BLOCKNODE.h 文件

#include <iostream>

using namespace std;

struct blocknode
{
unsigned int bsize;
bool free;
unsigned char *bptr;
blocknode *next;
blocknode *prev;

blocknode(unsigned int sz,unsigned char *b,bool f=true,blocknode
*p=0,blocknode *n=0):
bsize(sz),free(f),bptr(b),prev(p),next(n) {}
};

CPP 文件

#include <cassert>
#include <iostream>
#include <sstream>
#include <string>
#include "MemoryManager.h"

using namespace std;


ostream & operator<<(ostream & out,const MemoryManager &M)
{
blocknode *tmp = M.firstBlock;
assert(tmp);
while(tmp)
{
out << "[" << tmp->bsize << ",";
if (tmp->free)
out << "free] ";
else
out << "allocated] ";
if (tmp->next)
out << " -> ";
tmp = tmp->next;
}
return out;
}

MemoryManager::MemoryManager(unsigned int memtotal): memsize(memtotal)
{
baseptr = new unsigned char[memsize];
firstBlock = new blocknode(memsize,baseptr);
}

blocknode *MemoryManager::getFirstPtr()
{
return firstBlock;
}

unsigned char * MemoryManager::malloc(unsigned int request)
// Finds the first block in the list whose size is >= request
// If the block's size is strictly greater than request
// the block is split, with the newly create block being free.
// It then changes the original block's free status to false
{
blocknode * tmp = this->firstBlock;
assert(tmp);
while (tmp){
if (tmp->bsize >= request){
if (tmp->bsize > request){
splitBlock(tmp, request);
return tmp->bptr;
}
tmp->free = false;
return tmp->bptr;

}
tmp = tmp->next;
}

}

void MemoryManager::splitBlock(blocknode *p, unsigned int chunksize)
// Utility function. Inserts a block after that represented by p
// changing p's blocksize to chunksize; the new successor node
// will have blocksize the original blocksize of p minus chunksize and
// will represent a free block.
// Preconditions: p represents a free block with block size > chunksize
// and the modified target of p will still be free.

{
if (p->free == false || p->bsize <= chunksize) {
cout << "Error splitting memory....exiting with error code 1" << endl;
exit(1);
}
blocknode * heap = new blocknode(p->bsize,p->bptr + chunksize,true,0,0);
heap->bsize = p->bsize - chunksize;
heap->prev = p;
p->bsize = chunksize;
p->next = heap;
}

void MemoryManager::mergeForward(blocknode *p)
// merges two consecutive free blocks
// using a pointer to the first blocknode;
// following blocknode is deleted
{

blocknode * tmp = p->next;
p->bsize += p->next->bsize;
p->next = tmp->next;
tmp->next->prev = p;
delete tmp;

}


void MemoryManager::free(unsigned char *blockptr)
// makes the block represented by the blocknode free
// and merges with successor, if it is free; also
// merges with the predecessor, it it is free
{
blocknode * tmp = this->firstBlock->next;
assert(tmp);
while (tmp) {
if (tmp->bptr == blockptr) {
tmp->free = true;
if (tmp->free == true && tmp->next->free == true) {
mergeForward(tmp);
}
if (tmp->free == true && tmp->prev->free == true) {
mergeForward(tmp->prev);
}
}
}

这个程序的目标是模拟处理 malloc() 和 free() 的 C 堆管理器。我在使用内存管理器 cpp 文件的最后四个函数时遇到了问题。 (引用评论)代码编译但是我的程序在运行时崩溃,它说在内存位置 XxXXXXXXX 有一个未处理的异常有人知道是什么原因造成的吗?第 110 行(“if(tmp->next->free == true)”)是程序中断的地方

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com