有没有办法测试指针的内存分配是否已被释放?
有没有办法告诉指针的内存分配已被释放?我刚刚开始学习 C,我想我终于开始理解 C 中内存管理的复杂性。
例如:
char* pointer;
pointer = malloc(1024);
/* do stuff */
free(pointer);
/* test memory allocation after this point */
我知道指针仍然会存储内存地址,直到我设置 < code>pointer = NULL - 但是有没有办法测试指针不再引用我可以使用的内存,而不必先将其设置为 NULL?
我想这样做的原因是我对我的 C 程序进行了一堆单元测试,其中一个测试确保在我调用一个执行几个链表清理的特殊函数后不存在孤立指针。查看调试器,我可以看到我的清理功能正在工作,但我想要一种测试指针的方法,以便我可以将它们包装在单元测试断言中。
Is there a way to tell that a pointer's memory assignment has been deallocated? I am just starting out in C and I think I am finally starting to understand the intricacies of memory management in C.
So for example:
char* pointer;
pointer = malloc(1024);
/* do stuff */
free(pointer);
/* test memory allocation after this point */
I know that the pointer will still store the memory address until I set pointer = NULL
- but is there a way to test that the pointer no longer references memory I can use, without having to set it to NULL first?
The reason I want to do this is that I have a bunch of unit tests for my C program, and one of them ensures that there are no orphaned pointers after I call a special function which performs a cleanup of a couple of linked lists. Looking at the debugger I can see that my cleanup function is working, but I would like a way to test the pointers so I can wrap them in a unit test assertion.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
没有标准化的内存管理可以告诉您任何给定的地址是否是当前分配的内存块的一部分。
出于单元测试的目的,您可以创建一个全局映射来跟踪每个分配,这样您就可以使每个分配都通过自定义
malloc
函数,该函数在调试版本中向映射添加一个条目,并且在free
中再次删除它。There is no standardized memory management that tells you whether or not any given address is part of a currently allocated memory block.
For the purposes of a unit test, you could create a global map that keeps track of every allocation, so you make every allocation go through your custom
malloc
function that adds an entry to the map in debug builds and removes it again infree
.您始终可以出于单元的目的链接到库的仪表化版本(例如电围栏)测试。
不理想,因为您引入了生产和测试环境的差异。
有些系统可能在其标准库版本中提供足够的工具。例如,Mac OS 10.5 库支持在两次释放时调用
abort (3)
,因此,如果您的单元测试器可以捕获信号,您就可以自由了。无耻且毫无意义的自我推销:我的小玩具c单元测试框架可以捕获信号。
You could always link against an instrumented version of the libraries (say electric fence) for the purposes of unit testing.
Not ideal because you introduce a difference in the production and testing environments.
And some systems may provide sufficient instrumentation in their version of the standard library. For instance the Mac OS 10.5 library supports calling
abort (3)
on double frees, so if your unit tester can trap signals you are home free.Shameless and pointless self promotion: my little toy c unit testing framework can trap signals.
标准 C 和 POSIX(我认为)都没有提供检查的方法。您的特定操作系统可能有某种复杂的黑魔法来完成此操作,但仅向系统程序员的核心圈子透露。
Neither standard C nor POSIX (I think) provides a way to check that. Your specific operating system might have some sort of elaborate black magic for doing this that is only revealed to the Inner Circle of System Programmers, though.
使用良好的 C 实践。示例:
更长,是的,但是如果您始终将释放的指针设置为 NULL,则很容易测试。
Use good c practices. Example:
Longer, yes, but if you always set a freed pointer to NULL, it's easy to test.