Hi,
Say I've allocated some memory using malloc(), the operating system will give me the address to the memory allocated (i.e. a page). Now I am trying to browse this area of memory as if it was a 16-bit address space which starts from 0 and ends at 0xFFFF. (Don't ask why!, Just wanted to understand some concepts)
So I wrote this routine which will convert the so-called "virtual" address into a physical one:
uint32_t* virtual_to_physical(uint16_t OFFSET, uint32_t* MEMORY_BUFFER)
{
uint32_t* a = MEMORY_BUFFER + OFFSET;
return a;
}
Basically this routine will get the malloc()ed buffer and then will add the offset and return the physical address.
Now when I try it like this:
uint32_t* physaddr = (uint32_t*)malloc(0xFFFF);
printf("\nmalloc()ed buffer: %p", physaddr);
uint32_t* new_addr = virtual_to_physical(1, physaddr); printf("\nVirtual Address 1 turned to physical address: %p", (uint32_t*)new_addr);
But the output I get is:
malloc()ed buffer: 0x12b90a0
Virtual Address 1 turned to physical address: 0x12b90a4
Instead of adding 1 to the Physical address it increments it by 4?