Click here to Skip to main content
16,005,141 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionFunction to manipulate file attributes ? Pin
Still learning how to code19-Oct-02 6:35
Still learning how to code19-Oct-02 6:35 
AnswerRe: Function to manipulate file attributes ? Pin
valikac19-Oct-02 6:55
valikac19-Oct-02 6:55 
GeneralRe: Function to manipulate file attributes ? Pin
Still learning how to code19-Oct-02 9:53
Still learning how to code19-Oct-02 9:53 
GeneralRe: Function to manipulate file attributes ? Pin
valikac19-Oct-02 10:22
valikac19-Oct-02 10:22 
Generalwinxp compatibility issues Pin
Brad Jennings19-Oct-02 6:30
Brad Jennings19-Oct-02 6:30 
GeneralRe: winxp compatibility issues Pin
Brad Jennings19-Oct-02 6:41
Brad Jennings19-Oct-02 6:41 
GeneralRe: winxp compatibility issues Pin
Brad Jennings19-Oct-02 9:33
Brad Jennings19-Oct-02 9:33 
GeneralHeap Management !!!! Pin
tongc19-Oct-02 5:31
tongc19-Oct-02 5:31 
Hi !

I'm writing a heap mangement functions namely Alloc and Free. An array of 4096 character is simulated as the heap. The main progam which has been written call Alloc 60% of the time and Free 40% of the time. The heap management is implemented by the linked-list. The algorithm to choose the page is first fit. The Alloc is passed in the number of bytes to be allocated. The Free function takes in the pointer at the begining of the chunk to be allocated.

Here is what i've attempt at it. I define a linked-list structure with *next is the pointer to the next node, size is the size in byte of how big the chunk is.

<br />
<br />
#include <windows.h><br />
#include "ssHeap.h"<br />
<br />
<br />
typedef struct _HEAP_STRUCT<br />
{<br />
	DWORD size;<br />
	struct _HEAP_STRUCT *next;<br />
}HEAP_STRUCT;<br />
<br />
<br />
HEAP_STRUCT *front;<br />
<br />
HEAP_STRUCT *temp, *prevNode;<br />
HEAP_STRUCT *node;<br />
<br />
<br />
<br />
char HeapSpace[4096];<br />
LPVOID beginOfTheHeap;<br />
<br />
void ssInit(void)<br />
{<br />
	<br />
	front = new HEAP_STRUCT;<br />
	beginOfTheHeap = malloc(sizeof(HeapSpace));<br />
	front = (HEAP_STRUCT*)beginOfTheHeap;<br />
	<br />
	front->next = NULL;<br />
	front->size = 0;<br />
<br />
	node = new HEAP_STRUCT;<br />
	node->next = NULL;<br />
	node->size = 0;<br />
	<br />
	<br />
};<br />
<br />
<br />
LPVOID ssAlloc(LONG nBytes)<br />
{<br />
	HEAP_STRUCT *alloc = new HEAP_STRUCT;<br />
	int allocated = false;<br />
<br />
	while(nBytes % 8 != 0)	//make this multiple of 8.<br />
		nBytes++;	//if this is not multiple of 8, increase it <br />
	//for example if 31%8 =7, then it will allocate 32bytes.<br />
<br />
<br />
	if(front->next == NULL)	// this only happen if ssAlloc is called the first time<br />
	{<br />
<br />
		node = new HEAP_STRUCT;	//create a new node<br />
		node->next = NULL;	//new node ->next points to NULL		<br />
		node = front + nBytes;	//this node address is nBytes away from the front<br />
		<br />
		<br />
		front->size = nBytes;	//this new node's size is what is left off	<br />
		front->next = node;	<br />
		return node;<br />
	}<br />
<br />
	<br />
	<br />
	while(front->next != NULL)<br />
	{<br />
		long freeSpace = ((int)front->next - (int)(front + front->size))/sizeof(char);  //the free space between block<br />
	<br />
		if( freeSpace> nBytes)	//if the size is greater than nBytes<br />
		{<br />
			<br />
			while(freeSpace != nBytes)	//the size must be multiple of 8<br />
			{<br />
				freeSpace = freeSpace / 2;	//divide the free space to two<br />
			}<br />
<br />
			node = new HEAP_STRUCT;		//create a new node<br />
			node = front + front->size;	//the mem location of the new node;<br />
			node->size = nBytes;<br />
			node->next = front->next ;	//point to the next free space that temp point to<br />
			front->next = node;	//now this one is point to the node that is new allocated <br />
			alloc = node;				//for returning purpose.<br />
			<br />
			allocated = true;	//set the allocated flag.<br />
		}<br />
		else <br />
		{<br />
			front = front->next;<br />
		}<br />
	}<br />
			<br />
	<br />
	if(allocated)		//if the space is allocated <br />
		return LPVOID(alloc);<br />
	else				//if there is not allocated space<br />
		return NULL;<br />
<br />
};<br />
<br />
<br />
void ssFree(LPVOID p)<br />
{<br />
		<br />
	if(front == (HEAP_STRUCT*)p)	//First node can not deleted<br />
	{<br />
		front->size = 0;<br />
	}<br />
	else<br />
	{<br />
		prevNode = new HEAP_STRUCT;<br />
		temp = new HEAP_STRUCT;<br />
<br />
		<br />
		while(front->next->next != NULL)<br />
		{<br />
			prevNode = front;	//keep track of the previous node;<br />
<br />
			if(front->next ==(HEAP_STRUCT*)p)<br />
			{	<br />
				temp = front->next->next;<br />
				prevNode = temp;<br />
				delete front->next;<br />
			}<br />
<br />
			front->next = front->next->next ;	//go to the next of next node;<br />
		}<br />
	<br />
	}<br />
};<br />
<br />


Any constructive comment is highly appreciated!!

Regard,
GeneralRe: Heap Management !!!! Pin
Patje20-Oct-02 21:14
Patje20-Oct-02 21:14 
Generalmemory comparison Pin
tongc19-Oct-02 4:27
tongc19-Oct-02 4:27 
GeneralRe: memory comparison Pin
includeh1019-Oct-02 4:55
includeh1019-Oct-02 4:55 
GeneralRe: memory comparison Pin
Nish Nishant19-Oct-02 4:58
sitebuilderNish Nishant19-Oct-02 4:58 
GeneralRe: memory comparison Pin
Nish Nishant19-Oct-02 4:57
sitebuilderNish Nishant19-Oct-02 4:57 
GeneralRe: memory comparison Pin
Michael Dunn19-Oct-02 8:56
sitebuilderMichael Dunn19-Oct-02 8:56 
GeneralDisable the taskbar name of any process Pin
suresh_sathya19-Oct-02 4:26
suresh_sathya19-Oct-02 4:26 
GeneralRe: Disable the taskbar name of any process Pin
Nish Nishant19-Oct-02 4:53
sitebuilderNish Nishant19-Oct-02 4:53 
Generalabout modal and modaless dialog when dealing with large data Pin
whelk19-Oct-02 3:56
whelk19-Oct-02 3:56 
GeneralRe: about modal and modaless dialog when dealing with large data Pin
Stephane Rodriguez.19-Oct-02 4:02
Stephane Rodriguez.19-Oct-02 4:02 
GeneralRe: about modal and modaless dialog when dealing with large data Pin
Nish Nishant19-Oct-02 5:42
sitebuilderNish Nishant19-Oct-02 5:42 
Generalfloat format Pin
wong190719-Oct-02 3:55
wong190719-Oct-02 3:55 
GeneralRe: float format Pin
Stephane Rodriguez.19-Oct-02 4:03
Stephane Rodriguez.19-Oct-02 4:03 
GeneralRe: float format Pin
Nitron19-Oct-02 4:03
Nitron19-Oct-02 4:03 
QuestionRe: float format Pin
David Crow3-Oct-06 4:57
David Crow3-Oct-06 4:57 
Generalusing CreateProcess Pin
includeh1019-Oct-02 3:25
includeh1019-Oct-02 3:25 
GeneralRe: using CreateProcess Pin
Nish Nishant19-Oct-02 4:54
sitebuilderNish Nishant19-Oct-02 4:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.