C++ 中的 C 函数
C functions in c++
#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()
评论中指出的其他错误的补充。
相关文章:
- 用户定义函数中的指针和输入
- constexpr 函数中的非文字(通过 std::is_constant_evaluated)
- 修改函数中的指针(将另一个指针作为参数传递)
- 特征::矩阵<双精度,1,3> 结构类型函数中的返回类型函数
- rcpp函数中的清理时间很长
- 模板构造函数中的C++模板参数
- C++中构造函数中的枚举
- 自动重复 extern "C"函数中的类构造函数采用的相同参数
- C++模板函数中的初始化 - 新的初始值设定项表达式列表被视为复合表达式
- C++函数中的精确"return"矩
- 函数中的 c++ using 语句,后跟函数名称(对于 ADL?
- 构造函数中的 QQuickItem 父项 null
- 在 c++ 中将变量作为结构构造函数中的引用传递
- 线性优化目标函数中的绝对值
- 使用 SWIG 更改生成的 CS 函数中的返回类型
- 模板函数中的运算符<<重载
- 局部变量保留函数中的值
- 为什么 'main' 函数中的局部 int 变量会自动初始化?
- 使用指针算法修改函数中的 2D 数组
- 自定义排序函数中的堆溢出