In this section, we will discover how the windows manage the memory.

Allocating Memory Example

We have multiple ways to allocate memory addresses so let’s look at them.

Using malloc

#include <stdio.h>
#include <Windows.h>

int main()
{
	//This will allocate a memory with 100 bytes of space
	PVOID pAddress = malloc(100);
	if (pAddress != NULL)
	{
		printf("Address Allocated: %p",pAddress);
	}
	return EXIT_SUCCESS;
}

Untitled

Notice that we used the malloc function to allocate 100 bytes of space in memory.

Using HeapAlloc

#include <stdio.h>
#include <Windows.h>

int main()
{
	PVOID pAddress = HeapAlloc(GetProcessHeap(), 0, 100);
	if (pAddress != NULL)
	{
		printf("Address Allocated: %p",pAddress);
	}
	return EXIT_SUCCESS;
}

Untitled

As observed we successfully allocated the 100 bytes of space.

Now, take a look at the HeapAlloc function.

Untitled

The HeapAlloc allocates a block of memory in the heap.

The Function takes three parameters.