可能的重复:
我怎样才能得到来自 C 中指针的数组的大小?
有吗有什么方法可以以编程方式确定 C++ 数组的大小?如果没有,为什么?
我从一个 C 风格的函数中得到一个指向一块已分配内存的指针.现在,为了调试目的,知道如何big 这个指针指向的分配内存块.
I get a pointer to a chunk of allocated memory out of a C style function. Now, it would be really interesting for debugging purposes to know how big the allocated memory block that this pointer points is.
还有什么比盲目越界引发异常更优雅的事情吗?
Is there anything more elegant than provoking an exception by blindly running over its boundaries?
提前致谢,安德烈亚斯
我在 Windows 上使用 VC++2005,在 Linux 上使用 GCC 4.3
I use VC++2005 on Windows, and GCC 4.3 on Linux
我在 VC++2005 下有 _msize
不幸的是它在调试模式下导致异常......
I have _msize
under VC++2005
Unfortunately it results in an exception in debug mode....
嗯.我已经尝试过我上面描述的方法,但它确实有效.至少在我调试并确保在通话后立即到图书馆出口我跑过缓冲区边界.很有魅力.
Well. I have tried the way I described above with the exception, and it works. At least while I am debugging and ensuring that immediately after the call to the library exits I run over the buffer boundaries. Works like a charm.
它只是不优雅,也无法在生产代码中使用.
It just isn't elegant and in no way usable in production code.
这不是标准的,但如果你的库有一个 msize()
函数可以给你大小.
It's not standard but if your library has a msize()
function that will give you the size.
一个常见的解决方案是用您自己的函数包装 malloc
,该函数记录每个请求以及大小和结果内存范围,在发布版本中,您可以切换回真实"malloc
.
A common solution is to wrap malloc
with your own function that logs each request along with the size and resulting memory range, in the release build you can switch back to the 'real' malloc
.
这篇关于如何获取使用 malloc() 分配的内存块的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!