I have just upgraded to MacOS Monterey and I am seeing some unusual behaviour in terms of addresses returned when allocating memory on the heap (malloc/new). It seems that if you ask for more than 256 bytes you get an address just above PAGE_ZERO (0x100000000), but if you ask for 256 bytes or less you get an address above 0x600000000000.
I know that the c library is free to return whatever address it likes when allocating on the heap and this shouldn't matter to your code. But my code relies on the heap addresses being in the same area of memory. I know this is bad programming, but there are reasons for it.
Is there any way of making Monterey allocate memory in the same area of memory (aside from always allocating more than 256 bytes)? Thank you for any ideas.
Example code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
size_t i,count = 2;
for (i = 0; i < 16; i++)
{
void *ptr = malloc(count);
printf("malloc(%d) returned %p\n",count,ptr);
count *= 2;
}
for (count = 250; count < 260; count++)
{
void *ptr = malloc(count);
printf("malloc(%d) returned %p\n",count,ptr);
}
return 0;
}