I have a C++ class and I can create its object on stack and heap. How should I count the number of objects created on Heap and stack in C++. Can we do it by using static member variable and overloading new operator.
Asked
Active
Viewed 111 times
1
-
If you're compiler is using a stack. consider that when you pass parameters, the parameters are placed on the stack, along with the return address. Variables in a block statement may be pushed onto the stack, then when execution leaves the block, the variables are removed from the stack. – Thomas Matthews Apr 28 '20 at 18:02
-
1Does this answer your question? [Counting of objects created in stack and heap for many classes](https://stackoverflow.com/questions/3004963/counting-of-objects-created-in-stack-and-heap-for-many-classes) – faressalem Apr 28 '20 at 18:03
-
Are you asking to count the maximum number of objects pushed onto the stack at one point? Remember, the number of variables may also be affected by the depth of function calls and recursion. – Thomas Matthews Apr 28 '20 at 18:04
-
When you say "stack", what do you mean exactly? Do you mean the underlying stack mechanism inside your C++ implementation? Which includes function arguments and all sorts of stuff? Or do you mean C++ objects with automatic storage duration declared in your function? Let's use proper, precise terminology for clear answers! – Asteroids With Wings Apr 28 '20 at 18:11
-
Updated the question. – user3075456 Apr 28 '20 at 18:13
-
You can overload new/malloc and family to do this. It is a common debug tool in embedded land. – Michael Dorgan Apr 28 '20 at 19:41