C++ 中的 C 函数

C functions in c++

本文关键字:函数 中的 C++      更新时间:2023-10-16
#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("( 是程序中断的地方

MemoryManager::free()由于mergeForward()中发生的事情而调用mergeForward()(第一次调用mergeForward(((时,看起来free()使用的tmp指针将不再有效,因为mergeForward((delete它。

紧接着tmp的拒绝将导致未定义的行为。

这是我对free()评论中指出的其他错误的补充。