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

标签 c++ c methods

<分区>

#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() 删除它。

tmp 的取消引用将立即导致未定义的行为。

这是我在评论中指出的 free() 中的另一个错误的补充。

关于c++ - C++中的C函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35422821/

相关文章:

java - 尽管也可以通过 main 方法调用,但通过 netbeans 中的类构造函数调用 initComponents() 有何意义?

C++ 函数写得不好,不知道如何简化它[初学者]

c++ - 为什么正则表达式在 C++ 的日语字符串中找不到 "("?

c++模板,如何将模板参数映射到其他模板参数

c++ - 整数序列作为非类型模板参数

c++ - 访问集合的第 n 个元素

c++ - 两个 .c 文件具有相同的编译设置 - VC++ 报告没有错误并且不编译其中一个

c - ftell() 在附加模式下返回的文件指针位置的初始值

c - 在嵌入式系统上解析字符串的有效方法是什么?

c++ - 是否有围绕类添加方法的最薄包装器之类的东西?