Advertisement Jump to content


This topic is now archived and is closed to further replies.


Memory allocator??

This topic is 5341 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, I''m working on a memory allocator, but I''d run into some serious problems The allocator isn''t completed yet. The Malloc function is quite defective, but it will do.
class MallocWindow {

	MallocWindow() {

		mem = (MallocBlock*)malloc(POOL_MAX);
		mem->blocks = (POOL_MAX >> 3); mem->free = true; mem->next = NULL;
		firstMem = mem;



	void *Malloc(unsigned int capacity){

		// To get the number of blocks required for the request, we

		// round the capacity up to the nearest multiple of 8, then

		// divide it by 8 and add a 8 bytes buffer for the header.

		unsigned int blocks = (((capacity + 7) & ~7) >> 3) + 1;
		MallocBlock *currBlock = firstMem;

		// Loop through all the blocks to see if there''s any free blocks

		// with enough capacity.

		while (currBlock != NULL){

			if (currBlock->free)
				if (currBlock->blocks >= blocks)

			currBlock = currBlock->next;

		// When a block is found, it''s time to determine whether it

		// should be spilt or not. If the allocation will leave more

		// than 5 (40 bytes) unused blocks behind, the block is big

		// enough for splitting.

		if (currBlock->blocks - blocks > 5){

			MallocBlock *nextBlock = (currBlock + blocks);
			nextBlock->blocks = currBlock->blocks - blocks;
			nextBlock->free = true; nextBlock->next = currBlock->next;

			currBlock->blocks = blocks; currBlock->free = false;
			currBlock->next = nextBlock;

		// If the block was to small, we just use the entire one.

			currBlock->free = false;

		return (currBlock + 1);

	void Free(void *ptr){

		MallocBlock *currBlock = ((MallocBlock*)ptr) - 1;

		currBlock->free = true;

		currBlock = firstMem;

		while (currBlock != NULL){

			if (currBlock->free)
				if (currBlock->next->free){
					currBlock->blocks = currBlock->blocks + currBlock->next->blocks;
					currBlock->next = currBlock->next->next;

			currBlock = currBlock->next;

	void DumpAllocs(){

		MallocBlock *iterator = firstMem;
		ofstream log("Memory Log.txt", std::ios::trunc | std::ios::out);
		char buffer[1024]; unsigned int totalAllocs = 0;
		sprintf(buffer, "Memory pool: %d bytes -> %d blocks\n", POOL_MAX, POOL_MAX >> 3);
		log << buffer;
		sprintf(buffer, "------------------------------------------\n");
		log << buffer;

		while (iterator != NULL){

			sprintf(buffer, "Block #%d: %d blocks; free: %d\n",
					++totalAllocs, iterator->blocks, iterator->free);
			log << buffer;

			iterator = iterator->next;



	// Information about a block of memory. 8 bytes.

	struct MallocBlock {

		unsigned short blocks, free;
		MallocBlock *next;

	enum {POOL_MAX = 32768};
	MallocBlock *mem, *firstMem;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrev, PSTR cmdLine, int show){

	////////// *** Detection Memory Leaks *** //////////

	#ifndef NDEBUG
	int flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
	////////// *** Detection Memory Leaks *** //////////

	MallocWindow m;
	char *pBytes = (char*)m.Malloc(100);
	pBytes = "hej";
	int *pInts = (int*)m.Malloc(100);
	pInts[0] = 1;
	double *pDouble = (double*)m.Malloc(512);


	MSG msg;
	AppWindow appWindow;

	ASSERT(appWindow.InitWindow(), "The Window Wrapper Could Not Initialize The Window");

	while (true){

		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){

			if (msg.message == WM_QUIT)


		else {



	return msg.wParam;
It''s the freeing function, which is caursing my pain. It works when I don''t use the pointer (*pBytes), but intantly when I uncooment the line "pBytes = "hej";" it breaks.?? What could be wrong?

Share this post

Link to post
Share on other sites
i haven't read all your code but you can't do this:

char *pBytes = (char*)m.Malloc(100);
pBytes = "hej";

you have to do something like:

char *pBytes = (char*)m.Malloc(100);

//either this

sprintf(pBytes, "hej");

//or this

strcpy(pBytes, "hej");

//or some other way, perhaps using memset or something else

you can only set a char using = "string" during initialization:

char *myString = "myString";


[edited by - Palidine on June 2, 2004 5:47:55 PM]

Share this post

Link to post
Share on other sites
You''r right, but that wasn''t quite the problem. It seems like the pointer are giving error without being "used" before freed to. I really can''t see the problem?

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!