I've run into an issue with our code under clang involving realloc. This code works fine under gcc and visual studio, so I'm interested in understanding clang's behavior. Our code does something similar to this (error handling etc elided):
// allocate way more memory than we need
char * memory = malloc(500000);
int memoryused = 0;
// ... code goes here that fills up the memory, keeping track by
// setting memoryused ...
// now we're done, shrink the memory down to the size it needs
memory = realloc(memory, memoryused);
Under gcc and multiple versions of MSVC, the memory pointer wasn't moved. However, under clang, it appears that it moves the memory, even if the size needed is only 1-2000 bytes.
Is there a particular reason that clang does this? The only one I can think of is that perhaps it keeps separate heaps for large and small allocations or something. Otherwise it seems a bit inefficient to move the memory around, when it could just truncate the old memory block and continue using it without having to copy the data at all.