获取被调用函数的行号 [英] Gettin line number of the called function

查看:148
本文介绍了获取被调用函数的行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请让我知道是否可以做到这一点。

Please let me know if I can do it or not?

我正在写一个可以在C ++中跟踪内存分配和取消分配的库。总之,我试图看看我的应用程序没有任何内存泄漏。这是我到目前为止做的。

I am writing a library that could work to track memory allocation and de-allocation in C++. In short, I am trying to see if my application does not have any memory leaks. This is what I did so far.

覆盖新的和删除操作符,现在。每当应用程序使用新的,我计划存储地址和在该调用中分配的字节数。类似地,当在该地址上调用删除时,我将从存储的列表中删除它。直到现在它的罚款。但是我想存储文件名和从新被调用的行号。我可以做到这一点,只有在被覆盖的新。是否有办法获取重写的函数中的行号?

Overrode the new and the delete operator and now. Whenever the application uses new, I am planning to store the address and the number of bytes that are allocated in that call. Similarly, when the delete is called on that address, I will remove that from the stored list. Until now its fine. But I want to store the file name and the line number from where the "new" is called. I can do that only in the overridden new. Is there a way to get line number in the overridden function?

1    int main()
2    {
3       // Say, A is a structure
4        A *a = new A();
5        return 0;
6     }
7    void* operator new( size )
8    {
9        //
10       // store the line number and file name on line 4 ??? Can I do it?
11       return (malloc(size));
12   }
------------------------


推荐答案

__ LINE __ 给出行号
, code> __ FUNCTION __ 和 __ FILE __ 真的很有帮助,给出了封装函数的const char *和文件名。编辑:( __ FUNCTION __ 不是标准的,但是由多个编译器支持)
但是不幸的是这些宏只是将它们的值放到位。

the macros __LINE__ gives the line number but also __FUNCTION__ and __FILE__ are really helpful, giving the const char* of the enclosing function, and file name. edit: (__FUNCTION__ is not standard but supported by several compilers) But unfortunately these macros only take their value in place. So it is not possible for you to ask for the caller.

你应该写 __ LINE __ __ FILE __ 宏使用 new 。 :(。

You should write the __LINE__ and __FILE__ macros everywhere you use new. :(.

或者...你可以访问堆栈的调用者,但我不知道有什么常见的方法来做...和我已经搜索了很长时间,以至于认为如果不是在调试模式下是不可能的。

Or... you can have access to the stack of callers, but I don't know of any common ways to do that... and I had already searched for long so as to think it is not possible if not in debug mode.

这篇关于获取被调用函数的行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆