c++ - How to check a pointer is pointing heap or stack memory on iOS? -
this analogue another question, anyway looking platform specific way if exists on ios.
developing apple platform means non-apple based toolset not applicable. wish find platform native way this. because simple google search gave me this(heap
command), i'm sure there's api function too.
i looking debug build assertion detect case of deleting stack-allocated object. it's enough know address pointing - stack or heap. performance, version compatibility, internal api or quality concerns doesn't matter. (maybe testing on simulator can option) think not heavy operation if stack separated heap.
i tagged c++, api in other language fine if applicable c++.
if use gnu gcc compiler , glibc
on ios believe can use mprobe()
- if fails memory block either corrupted or stack memory block.
http://www.gnu.org/software/libc/manual/html_node/heap-consistency-checking.html
updated post os portable heap detection:
otherwise, make own heap memory manager overriding new()
&delete()
, record heap memory allocations/deallocations, add own heap detection function; example follows:
// untested pseudo code follows: // #include <mutex> #include <map> #include <iostream> std::mutex g_i_mutex; std::map<size_t, void*> heaplist; void main() { char var1[] = "hello"; char *var2 = new char[5]; if (isheapblock(&var1)) std::cout "var1 allocated on heap"; else std::cout "var1 allocated on stack"; if (isheapblock(var2)) std::cout "var2 allocated on heap"; else std::cout "var2 allocated on stack"; delete [] var2; } // register heap block , call malloc(size) void *operator new(size_t size) { std::lock_guard<std::mutex> lock(g_i_mutex); void *blk = malloc(size); heaplist.add((size_t)blk, blk); return blk; } // free memory block void operator delete(void *p) { std::lock_guard<std::mutex> lock(g_i_mutex); heaplist.erase((size_t)p); free(p); } // returns true if p points start of heap memory block or false if p // stack memory block or non-allocated memory bool isheapblock(void *p) { std::lock_guard<std::mutex> lock(g_i_mutex); return heaplist.find((size_t)p) != heaplist.end(); } void *operator new[] (size_t size) { return operator new(size); } void operator delete[] (void * p) { operator delete(p); }
Comments
Post a Comment