There is no portable way. However, since most implementations allocate heap objects in a different memory region than literal (string) data, it is possible to make an educated guess if a given string pointer falls within either region.
static char x;
bool isStatic(const void *p)
{
const char * cp = (const char *)p;
// Check if the pointer falls within +/-16K of 'x'
if (cp-16*1024 <= &x && &x <= cp+16*1024)
return true;
else
return false;
}
Obviously, this is a bit of a hack. A much better way would be to directly access the .bss
, .data
, and .text
addresses (these are Unix, Win32 is similar) of your executable after it is loaded into memory, and compare your pointers to those regions.