問題描述
我想知道一個指針是否指向一塊用malloc/new分配的內存.我意識到任意地址的答案是不,你不能",但我確實認為可以覆蓋 malloc/free 并跟蹤分配的內存范圍.
您知道提供此特定工具的內存管理庫嗎?
您了解生產代碼嗎?
Valgrind 很棒,但儀器太多(慢),正如 Will 所說,我們不想像這樣使用 Valgrind(使軟崩潰就足夠了).
Mudflap 是一個非常好的解決方案,但專用于 GCC,遺憾的是,檢查并沒有簡單地返回一個布爾值(見下面我的回答).
請注意,檢查內存寫入是否合法是一個安全問題.所以追求性能是有動力的.
沒有標準的方法可以做到這一點,但各種 malloc 調試工具可能有辦法做到這一點.例如,如果你使用valgrind,你可以使用VALGRIND_CHECK_MEM_IS_ADDRESSABLE
來檢查這個和相關的事情>
I want to know if a pointer points to a piece of memory allocated with malloc/new. I realize that the answer for an arbitrary address is "No you can't" but I do think it is possible to override malloc/free and keep track of allocated memory ranges.
Do you know a memory management library providing this specific tool?
Do you know something for production code?
Valgrind is great, but it is too much instrumentation (slow) and as Will said we don't want to use Valgrind like this (making the soft crash is good enough).
Mudflap is a very good solution, but dedicated to GCC, and sadly, a check does not simply return a boolean (see my answer below).
Note that checking that memory writes are legal is a security issue. So looking for performance is motivated.
There's no standard way to do this, but various malloc debugging tools may have a way of doing it. For example, if you use valgrind, you can use VALGRIND_CHECK_MEM_IS_ADDRESSABLE
to check this and related things
這篇關于檢查一個指針是否指向堆上分配的內存的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!