answersLogoWhite

0


Best Answer

You can not check whether a variable (object) is allocated on the stack or heap in CPP programmatically.

If you are being passed the address of an object, you may not make any assumptions about how that object was allocated unless you are also passed information about that allocation. The "owner" or allocator of the object knows how that object was allocated. The "user" of the object does not know, unless told, and should not even care.

If you want to write code that transfers "ownership" of an object to another "owner", you may, but that new "owner" must be told how to manage the object.

It is possible to look at the address of the object, and infer locality based the value of that address, but that is extremely fragile, because it is implementation specific. Do not even consider it.

It IS possible to create a class that can track whether it was allocated on the stack or heap. To do so, you need to override the desired operater new() and operate delete() functions in your class, such that your class will be allocating instances of the class itself. Then you need a means to signal from the operator new() function back to the class instance that it was allocated via new (since operator new executes before the class constructor). One simple way to do this is to keep a global list of all the objects you've created for the class (via operator new); the class constructor can then lookup the this pointer in the list to see if it was allocated with new, and set a flag accordingly. In this case, operator delete() would need to remove items from the list.

If your program is multi-threaded, access to this global list would either need to be protected by a semaphore or the list could reside in thread-local storage.

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How to check whether variable is allocated on stack or heap in CPP programmatically?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What are ing variable annuity good for?

You have a wide variety to choose from annually. That of course is the greatest benefit of all. Check with your local bank as to whether they provide them.


How can you check to see if a number is part of the solution set of an inequality?

You replace the variable by the number, do all specified calculations, and then check whether the resulting inequality is true or false. This is basically not very different from checking a solution of an equation.


Distinguish between measurable variable and non-measurable variables of a product?

One way is to check whether the pre-image of the product is sigma-algebra. Please list an example to clarify your question.


How do you check whether we are lack of oxygen?

We can check by breathing and we will feel difficult to breathe then this is how we check whether we are lack of oxygen.


What would i do to help me determine if something is metal?

Check the conductivity. Check whether it is lustrous. Check whether it is malleable.


Can you check crankshaft sensor?

only the engines with computers and electronic controls


What does ins mean on a social security check?

"INS" on a social security check typically stands for "Insurance." It indicates that a portion of the check amount is allocated towards paying for insurance coverage, such as Medicare premiums.


How can you check if a variable is a string or an integer in PHP?

Integers - The "is_int()" function can be used to check if a variable is has an integer for its value. ---- is_int($variable); // Returns true if $variable is an integer - otherwise false ---- Numeric Strings - Numeric strings are strings with numbers (or things that count as numbers) in them. Numeric-string variables are not integer variables. Numeric-string variables are passed on through forms, instead of integer variables - if you were wondering. Check form values using string formats, and not integer formats. The "is_numeric()" function can be used to check if a variable is a string with numbers - and only numbers - in it (except things that add up to be numbers). ---- is_numeric($variable); // Returns true if $variable is a string, and only contains numbers (broadly speaking) - false otherwise ---- Strings - String values are just text, basically. String variables can contain integers, but that does not make it an integer-type variable - it makes it a numeric string variable. The "is_string" function can be used to check if a variable contains the value of a string. ---- is_string($variable); // Returns true if $variable is a string - false otherwise


What is variable rental lease?

how much rent and do you check credit get the apartment


I looking for vin check up for car whether it was flooded or not?

I am looking for a vin check up fro a car whether it was flooded or not flooded check up


How do you check whether your new N96 is original Nokia Finland?

How do you check whether your new N96 is original Nokia Finland?


What is definition and examples of dependent variable and independent variable?

I'm doing a science project on if fingerprint patterns are inherited? what is the control, independent variable and dependent variable? An ''independent variable is a ''variable which is presumed to affect or determine a dependent ''variable. independent variable is typically the variable being manipulated or changed and the dependent variable is the observed result of the independent variable being manipulated.'''''' Example; In price and sales relationship one can manipulate price to check its effect on sales, so sale is dependent and price is independent variable.