Please let me know if I can do it or not?
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 }
------------------------
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…